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(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary 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 scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info See in Glossary. |
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 IL2CPPA Unity-developed scripting back-end which you can use as an alternative to Mono when building projects for some platforms. More info See in Glossary scripting backendA framework that powers scripting in Unity. Unity supports three different scripting backends depending on target platform: Mono, .NET and IL2CPP. Universal Windows Platform, however, supports only two: .NET and IL2CPP. More info See in Glossary 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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.