AppCallbacks クラスを使用して、メインアプリケーションを Unity エンジンに接続できます。
AppCallbacks クラスの使用方法App.xaml.cpp ファイルApp::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 ファイルMainPage::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 は UI スレッドでアプリケーションを実行しません。大きなアプリケーションをロードする際に UI が応答しなくなる可能性があるためです。UI スレッドの詳細については、Microsoft のドキュメントで Keeping the UI thread responsive を参照してください。
m_AppCallbacks = ref new AppCallbacks(); を使用して AppCallbacks クラスを作成すると、Unity は App Thread という新しいスレッドを作成します。この新しいスレッドは、Microsoft の制限により作成されます。5 秒経過してもアプリケーションが応答しないと、Windows App Certification Kit のテストに合格できなくなるためです。詳細については、Microsoft のドキュメントで Windows アプリケーション認定キットを参照してください。
注意App.xaml.cpp ファイルおよび MainPage.xaml.cpp ファイル内のコードは、InvokeOnAppThread 関数から呼び出されない限り、常に UI スレッドで実行されます。
カスタムのコマンドライン引数を文字列配列として AppCallbacks コンストラクターに渡すことができます。詳細は、UWP コマンドライン引数を参照してください。
| 機能 | 説明 |
|---|---|
appCallbacks->InitializeD3DXAML(); |
DirectX 11 デバイスを初期化し、最初のレベルをロードします。 |
appCallbacks->SetCoreWindowEvents(Window::Current->CoreWindow); |
Unity のコアウィンドウを設定します。Unity は以下のシステムイベントをサブスクライブします。 - VisibilityChanged - Closed - PointerCursor - SizeChanged - Activated - CharacterReceived - PointerPressed - PointerReleased - PointerMoved - PointerCaptureLost - PointerWheelChanged - AcceleratorKeyActivated |
appCallbacks->SetSwapChainPanel(m_DXSwapChainPanel); |
Unity に XAML コントロールを渡して、DirectX 11 のレンダーターゲットとして使用します。 |
void GetSwapChainPanel() |
SwapChainPanel オブジェクトを返します。SetSwapChainPanel メソッドで設定できます。 |
void Initialized() |
メインのゲームループを実行するのに十分な初期化がエンジンに対して行われているかどうかを返します。 |
void InitializeD3DWindow() |
D3D アプリケーション用のエンジングラフィックス、DirectX、ゲームパッドおよびジョイスティックの入力を初期化します。 |
void Instance() |
以前に作成した AppCallbacks オブジェクトのシングルトンインスタンスを取得します。 |
void InvokeOnAppThread(AppCallbackItem item, bool waitUntilDone) |
アプリケーションスレッドでデリゲートを呼び出します。この機能は、UI スレッドからスクリプト機能を実行する場合に便利です。 |
void InvokeOnUIThread(AppCallbackItem item, bool waitUntilDone) |
UI スレッドでデリゲートを呼び出します。この機能は、スクリプトから XAML 固有の API を呼び出す場合に便利です。 |
bool IsInitialized() |
アプリケーションの最初のレベルが完全にロードされたときに true を返します。 |
void RenderingStarted() |
Unity が最初のフレームをレンダリングした後に開始します。 |
void Run() |
D3D アプリケーションがメインループに入るようにします。 |
bool RunningOnAppThread() |
アプリケーションスレッドで実行されている場合は true を返します。 |
bool RunningOnUIThread() |
UI スレッドで実行されている場合は true を返します。 |
void SetAppArguments(string arg) / string GetAppArguments()
|
アプリケーション引数を設定します。UnityEngine.WSA.Application.arguments からアクセスすることができます。 |
void SetCoreApplicationViewEvents() |
CoreApplicationView::Activated イベントをサブスクライブし、IL2CPP スクリプティングバックエンドとグラフィックスを除くすべてのエンジンサブシステムをロードします。 |
bool UnityGetInput() |
Unity が入力を処理できる場合 true を返します。 |
void UnitySetInput(bool enabled) |
入力処理を有効または無効にします。 |
bool UnityPause(int pause) |
1 を渡すと Unity が一時停止し、0 を渡すと一時停止が解除されます。この機能は、ゲームを一時的にフリーズしたい場合に便利です。 |