You can use the AppCallbacks
class to connect your main application to the Unity engine.
AppCallbacks
classApp.xaml.cpp
fileApp::App()
{
InitializeComponent();
SetupOrientation();
m_AppCallbacks = ref new AppCallbacks();
}
void App::OnLaunched(LaunchActivatedEventArgs^ e)
{
m_SplashScreen = e->SplashScreen;
InitializeUnity(e->Arguments);
}
void App::InitializeUnity(String^ args)
{
ApplicationView::GetForCurrentView()->SuppressSystemOverlays = true;
m_AppCallbacks->SetAppArguments(args);
auto rootFrame = safe_cast<Frame^>(Window::Current->Content);
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == nullptr && !m_AppCallbacks->IsInitialized())
{
rootFrame = ref new Frame();
Window::Current->Content = rootFrame;
#if !UNITY_HOLOGRAPHIC
Window::Current->Activate();
#endif
rootFrame->Navigate(TypeName(MainPage::typeid ));
}
Window::Current->Activate();
}
MainPage.xaml.cpp
fileMainPage::MainPage()
{
m_SplashScreenRemovalEventToken.Value = 0;
m_OnResizeRegistrationToken.Value = 0;
InitializeComponent();
NavigationCacheMode = ::NavigationCacheMode::Required;
auto appCallbacks = AppCallbacks::Instance;
bool isWindowsHolographic = false;
#if UNITY_HOLOGRAPHIC
// If application was exported as Holographic check if the device actually supports it
// Otherwise, we treat this as a normal XAML application
isWindowsHolographic = AppCallbacks::IsMixedRealitySupported();
#endif
if (isWindowsHolographic)
{
appCallbacks->InitializeViewManager(Window::Current->CoreWindow);
}
else
{
m_SplashScreenRemovalEventToken = appCallbacks->RenderingStarted += ref new RenderingStartedHandler(this, &MainPage::RemoveSplashScreen);
appCallbacks->SetSwapChainPanel(m_DXSwapChainPanel);
// Subscribes to all needed system events
appCallbacks->SetCoreWindowEvents(Window::Current->CoreWindow);
// This is the main initialization function for Unity
// Initializes engine graphics, DirectX, and gamepad and joystick input
// Loads IL2CPP and all engine subsystems except graphics
appCallbacks->InitializeD3DXAML();
// At this point, when Unity finishes loading the first level, it enters the main loop.
m_SplashScreen = safe_cast<App^>(App::Current)->GetSplashScreen();
auto dispatcher = CoreWindow::GetForCurrentThread()->Dispatcher;
ThreadPool::RunAsync(ref new WorkItemHandler([this, dispatcher](IAsyncAction^)
{
GetSplashBackgroundColor(dispatcher);
}));
OnResize();
m_OnResizeRegistrationToken = Window::Current->SizeChanged += ref new WindowSizeChangedEventHandler([this](Object^, WindowSizeChangedEventArgs^)
{
OnResize();
});
}
}
Unity doesn’t run your app on the UI thread because the UI could become unresponsive when loading large apps. For more information on UI threads, refer to Microsoft documentation on Keeping the UI thread responsive.
When you create the AppCallbacks
class using m_AppCallbacks = ref new AppCallbacks();
, Unity creates a new thread called App Thread
. Unity creates this new thread due to a Microsoft restriction: if your application doesn’t become responsive after 5 seconds, you’ll fail to pass the Windows App Certification Kit tests. For more information, refer to Microsoft documentation on the Windows App Certification Kit.
Note: Code located in the App.xaml.cpp
and MainPage.xaml.cpp
files always runs on the UI thread, unless called from the InvokeOnAppThread
function.
You can pass custom command line arguments as string arrays into the AppCallbacks constructor. For more information, refer to UWP Command line arguments.
Function | Description |
---|---|
appCallbacks->InitializeD3DXAML(); |
Initializes your DirectX 11 device and loads the first level. |
appCallbacks->SetCoreWindowEvents(Window::Current->CoreWindow); |
Sets the core window for Unity. Unity subscribes to the following system events: - VisibilityChanged - Closed - PointerCursor - SizeChanged - Activated - CharacterReceived - PointerPressed - PointerReleased - PointerMoved - PointerCaptureLost - PointerWheelChanged - AcceleratorKeyActivated |
appCallbacks->SetSwapChainPanel(m_DXSwapChainPanel); |
Passes a XAML control to Unity which is used as a render target for DirectX 11. |
void GetSwapChainPanel() |
Returns the SwapChainPanel object, which you can set via the SetSwapChainPanel method. |
void Initialized() |
Returns whether the engine is initialized enough to run the main game loop. |
void InitializeD3DWindow() |
Initializes engine graphics, DirectX, and gamepad and joystick input for D3D applications. |
void Instance() |
Retrieves a singleton instance of a previously created AppCallbacks object. |
void InvokeOnAppThread(AppCallbackItem item, bool waitUntilDone) |
Invokes a delegate on the application thread. This function is useful when you want to execute your script function from a UI thread. |
void InvokeOnUIThread(AppCallbackItem item, bool waitUntilDone) |
Invokes a delegate on the UI thread. This function is useful when you want to invoke an XAML-specific API from your scripts. |
bool IsInitialized() |
Returns true when the first level of your application is fully loaded. |
void RenderingStarted() |
Starts after Unity renders its first frame. |
void Run() |
Enables D3D applications to enter the main loop. |
bool RunningOnAppThread() |
Returns true if you’re currently running in an application thread. |
bool RunningOnUIThread() |
Returns true if you’re currently running in a UI thread. |
void SetAppArguments(string arg) / string GetAppArguments()
|
Sets your application arguments, which you can then access from UnityEngine.WSA.Application.arguments. |
void SetCoreApplicationViewEvents() |
Subscribes to the CoreApplicationView::Activated event and loads the IL2CPP scripting backend and all engine subsystems except graphics. |
bool UnityGetInput() |
Returns true if Unity processes incoming input. |
void UnitySetInput(bool enabled) |
Enables or disables input processing. |
bool UnityPause(int pause) |
Pauses Unity if you pass 1 and unpauses if you pass 0. This function is useful if you want to temporarily freeze your game. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.