Version: 2019.3
언어: 한국어
유니버설 Windows 플랫폼: 연관 실행
유니버설 Windows 플랫폼: C# 스크립트의 WinRT API

AppCallbacks 클래스

AppCallbacks 클래스는 메인 애플리케이션과 Unity 엔진 사이의 브리지 역할을 합니다. 여기서는 AppCallbacks에 대한 호출이 각각 어떤 역할을 하는지 살펴볼 것입니다. 이제 솔루션을 빌드하고 App.xaml.cpp 및 MainPage.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::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);
        appCallbacks->SetCoreWindowEvents(Window::Current->CoreWindow);
        appCallbacks->InitializeD3DXAML();

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

m_AppCallbacks = ref new AppCallbacks();

AppCallbacks 클래스를 자세히 살펴보겠습니다. 이 클래스를 생성하면 Unity는 “AppThread”라고 불리는 새로운 스레드를 생성합니다. 그 이유는 Microsoft의 제약에 있습니다. 애플리케이션이 5초 뒤 응답하지 않으면 Windows 애플리케이션 인증인 WACK에 부합하지 못하기 때문입니다. (자세한 내용은 http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh184840(v=vs.105).aspx를 참조하십시오.) 첫 레벨이 상당히 크다고 가정하면 애플리케이션이 UI 스레드에서 실행되고 있으므로 로드하는 데 상당한 시간을 필요로 합니다. 따라서 레벨이 완전히 로드되기 전까지는 UI가 응답이 없게 됩니다. 이로 인해 게임을 다른 스레드에서 실행하는 것이 좋습니다.

UI 스레드에 대한 자세한 내용은 http://msdn.microsoft.com/en-us/library/windows/apps/hh994635.aspx를 참조하십시오.

커스텀 커맨드 라인 인자를 문자열 배열로 AppCallbacks 생성자에 전달할 수도 있습니다.

참고: InvokeOnAppThread 함수에서 호출되지 않는 한 App.xaml.cpp, MainPage.xaml.c[[에 있는 코드는 항상 UI 스레드에서 실행됩니다.

appCallbacks->SetSwapChainPanel(m_DXSwapChainPanel);

이 코드는 DirectX 11 렌더 타겟으로 사용될 XAML 컨트롤을 Unity에 전달합니다.

appCallbacks->SetCoreWindowEvents(Window::Current->CoreWindow);

Unity의 코어 창을 설정합니다. Unity는 아래의 이벤트에서 정보를 받습니다(리스트에 없는 새로운 이벤트가 있을 수도 있습니다).

  • VisibilityChanged
  • Closed
  • PointerCursor
  • SizeChanged
  • Activated
  • CharacterReceived
  • PointerPressed
  • PointerReleased
  • PointerMoved
  • PointerCaptureLost
  • PointerWheelChanged
  • AcceleratorKeyActivated

appCallbacks->InitializeD3DXAML();

Unity 메인 초기화 함수이며, 아래의 작업을 수행합니다.

  • DirectX 11 기기를 초기화합니다.
  • 첫 레벨을 로드합니다.

이 시점에서 Unity가 첫 레벨 로드를 완료하면 메인 루프에 진입합니다.

기타 함수

  • void InvokeOnAppThread(AppCallbackItem item, bool waitUntilDone)

애플리케이션 스레드 델리게이트를 호출합니다. UI 스레드에서 스크립트 함수를 실행하고 싶은 경우 유용합니다.

  • void InvokeOnUIThread(AppCallbackItem item, bool waitUntilDone)

애플리케이션 스레드 델리게이트를 호출합니다. 스크립트에서 XAML 전용 API를 호출하고 싶은 경우 유용합니다.

  • bool RunningOnAppThread()

현재 애플리케이션 스레드에서 실행 중인 경우 true값을 반환합니다.

  • bool RunningOnUIThread()

현재 UI 스레드에서 실행 중인 경우 true값을 반환합니다.

  • void InitializeD3DWindow()

D3D 애플리케이션 초기화 함수입니다.

  • void Run()

D3D 애플리케이션이 메인 루프에 진입할 때 사용하는 함수입니다.

  • bool IsInitialized()

첫 레벨이 완전히 로드되면 true값을 반환합니다.

  • void AddCommandLineArg(string arg)

애플리케이션의 커맨드 라인 인자를 설정합니다. nitializeD3DWindow, InitializeD3DXAML이전에 호출되어야 합니다.

  • void SetAppArguments(string arg) / string GetAppArguments()

애플리케이션 인자를 설정하며, 나중에 Unity API에서 UnityEngine.WSA.Application.arguments를 통해 접근할 수 있습니다.

  • void LoadGfxNativePlugin(string pluginFileName)

이 함수는 더 이상 사용되지 않으며 실질적인 기능이 없습니다. Unity 이전 버전에서는 UnityRenderEvent와 같이 콜백의 네이티브 플러그인을 등록하는 데 사용되었습니다. 이제 모든 플러그인은 자동으로 등록됩니다. 함수는 추후 업데이트에서 제거될 예정입니다.

  • void ParseCommandLineArgsFromFiles(string fileName)

파일에서 커맨드 라인 인자를 분석하며, 이때 인자는 빈 칸으로 분리되어야 합니다.

  • bool UnityPause(int pause)

1을 전달하면 Unity를 일시정지하며, 0을 전달하면 일시정지를 해제합니다. 게임이 끊긴 경우와 같이 게임을 일시정지하고 싶은 경우 유용합니다.

  • void UnitySetInput(bool enabled)

입력을 활성화하거나 비활성화합니다.

  • bool UnityGetInput()

Unity가 새로운 입력을 처리하는 경우 true값을 반환합니다.

  • void SetKeyboardTriggerControl(Windows.UI.Xaml.Controls.Control ctrl)

화상 키보드를 트리거하는 데 사용되는 컨트롤을 설정합니다. 이 컨트롤은 스크립트에서 화상 키보드를 요청하는 경우 단순 포커스를 받게 됩니다. 포커스가 주어지면 키보드를 여는 컨트롤과 동시에 호출되어야 합니다.

  • Windows.UI.Xaml.Controls.Control GetKeyboardTriggerControl()

현재 키보드 입력을 트리거하는 데 사용되는 컨트롤을 반환합니다. SetKeyboardTriggerControl를 참조하십시오.

  • void SetCursor(Windows.UI.Core.CoreCursor cursor)

시스템 커서를 설정합니다. 설정된 커서는 CoreWindow와 사용되는 경우 독립 입력 소스에 적용됩니다.

  • void SetCustomCursor(unsigned int id)

시스템 커서를 커스텀으로 설정합니다. 여기서 파라미터는 커서 리소스 ID입니다. 설정된 커서는 CoreWindow와 사용되는 경우 독립 입력 소스에 적용됩니다.

유니버설 Windows 플랫폼: 연관 실행
유니버설 Windows 플랫폼: C# 스크립트의 WinRT API