AppCallbacks class reference
Call the AppCallbacks class in your UWP application.
Read time 4 minutesLast updated 13 days ago
You can use the class to connect your main application to the Unity engine.
AppCallbacksExample: How to use the AppCallbacks
class
AppCallbacksApp.xaml.cpp
file
App.xaml.cppApp::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; Window::Current->Activate(); rootFrame->Navigate(TypeName(MainPage::typeid )); } Window::Current->Activate();}
MainPage.xaml.cpp
file
MainPage.xaml.cppMainPage::MainPage(){ m_SplashScreenRemovalEventToken.Value = 0; m_OnResizeRegistrationToken.Value = 0; InitializeComponent(); NavigationCacheMode = ::NavigationCacheMode::Required; auto appCallbacks = AppCallbacks::Instance; 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(); });}
Create an app thread
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 class using , Unity creates a new thread called . 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.
AppCallbacksm_AppCallbacks = ref new AppCallbacks();App ThreadCommand line arguments
You can pass custom command line arguments as string arrays into the AppCallbacks constructor. For more information, refer to UWP Command line arguments.
AppCallbacks functions
Function | Description |
|---|---|
| Initializes your DirectX 11 device and loads the first level. |
| 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 |
| Passes a XAML control to Unity which is used as a render target for DirectX 11. |
| Returns the SwapChainPanel object, which you can set via the SetSwapChainPanel method. |
| Returns whether the engine is initialized enough to run the main game loop. |
| Initializes engine graphics, DirectX, and gamepad and joystick input for D3D applications. |
| Retrieves a singleton instance of a previously created AppCallbacks object. |
| Invokes a delegate on the application thread. This function is useful when you want to execute your script function from a UI thread. |
| Invokes a delegate on the UI thread. This function is useful when you want to invoke an XAML-specific API from your scripts. |
| Returns true when the first level of your application is fully loaded. |
| Starts after Unity renders its first frame. |
| Enables D3D applications to enter the main loop. |
| Returns true if you’re currently running in an application thread. |
| Returns true if you’re currently running in a UI thread. |
| Sets your application arguments, which you can then access from UnityEngine.WSA.Application.arguments. |
| Subscribes to the CoreApplicationView::Activated event and loads the IL2CPP scripting backend and all engine subsystems except graphics. |
| Returns true if Unity processes incoming input. |
| Enables or disables input processing. |
| Pauses Unity if you pass 1 and unpauses if you pass 0. This function is useful if you want to temporarily freeze your game. |