# AppCallbacks クラスリファレンス

> Call the AppCallbacks class in your UWP application.

`AppCallbacks` クラスを使用して、メインアプリケーションを Unity エンジンに接続できます。

## 例`AppCallbacks` クラスの使用方法##example:-how-to-use-the-appcallbacks-class

### `App.xaml.cpp` ファイル##app.xaml.cpp-file

```
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.xaml.cpp-file

```
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();
        });
    }
}
```

## アプリケーションスレッドの作成##create-an-app-thread

Unity は UI スレッドでアプリケーションを実行しません。大きなアプリケーションをロードする際に UI が応答しなくなる可能性があるためです。UI スレッドの詳細については、Microsoft のドキュメントで [Keeping the UI thread responsive](https://learn.microsoft.com/en-gb/windows/uwp/debug-test-perf/keep-the-ui-thread-responsive) を参照してください。

`m_AppCallbacks = ref new AppCallbacks();` を使用して `AppCallbacks` クラスを作成すると、Unity は `App Thread` という新しいスレッドを作成します。この新しいスレッドは、Microsoft の制限により作成されます。5 秒経過してもアプリケーションが応答しないと、Windows App Certification Kit のテストに合格できなくなるためです。詳細については、Microsoft のドキュメントで [Windows アプリケーション認定キット](https://learn.microsoft.com/en-us/windows/uwp/debug-test-perf/windows-app-certification-kit)を参照してください。

**注意**`App.xaml.cpp` ファイルおよび `MainPage.xaml.cpp` ファイル内のコードは、`InvokeOnAppThread` 関数から呼び出されない限り、常に UI スレッドで実行されます。

## コマンドライン引数##command-line-arguments

カスタムのコマンドライン引数を文字列配列として AppCallbacks コンストラクターに渡すことができます。詳細は、[UWP コマンドライン引数](https://docs.unity3d.com/2023.1/Documentation/Manual/windowsstore-commandlinearguments.html)を参照してください。

## AppCallbacks 関数##appcallbacks-functions

| **機能**                                                             | **説明**                                                                                                                                                                                                                                                    |
| ------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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](/engine/6000.0/script-reference/unity-engine-wsa/application/arguments.md) からアクセスすることができます。                                                                                                      |
| `void SetCoreApplicationViewEvents()`                              | CoreApplicationView::Activated イベントをサブスクライブし、IL2CPP スクリプティングバックエンドとグラフィックスを除くすべてのエンジンサブシステムをロードします。                                                                                                                                                       |
| `bool UnityGetInput()`                                             | Unity が入力を処理できる場合 true を返します。                                                                                                                                                                                                                             |
| `void UnitySetInput(bool enabled)`                                 | 入力処理を有効または無効にします。                                                                                                                                                                                                                                         |
| `bool UnityPause(int pause)`                                       | 1 を渡すと Unity が一時停止し、0 を渡すと一時停止が解除されます。この機能は、ゲームを一時的にフリーズしたい場合に便利です。                                                                                                                                                                                       |

## 追加リソース##additional-resources

* [Microsoft ドキュメント: UI スレッドの応答性の維持](https://learn.microsoft.com/en-gb/windows/uwp/debug-test-perf/keep-the-ui-thread-responsive)
* [UWP コマンドライン引数](/engine/6000.0/manual/platform-specific/windows-store/uwp-developing/windowsstore-commandlinearguments.md)
* [コマンドライン引数](/engine/6000.0/manual/unity-editor/editor-features/command-line-arguments/player.md)
