Version: 6000.3
语言: 中文
用于 UWP 的 C# 脚本中的 WinRT API
UWP 的命令行参数

AppCallbacks 类引用

您可以使用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 线程的详细信息,请参阅有关保持 UI 线程响应的 Microsoft 文档。

当您创建AppCallbacks类使用m_AppCallbacks = ref new AppCallbacks();,Unity 会创建一个名为App Thread.由于 Microsoft 限制,Unity 创建了这个新线程:如果应用程序在 5 秒后未响应,则无法通过 Windows 应用认证工具包测试。有关详细信息,请参阅有关 Windows 应用认证工具包的 Microsoft 文档。

注意:代码位于App.xaml.cppMainPage.xaml.cpp文件始终在 UI 线程上运行,除非从InvokeOnAppThread功能。

命令行参数

您可以将自定义命令行参数作为字符串数组传递到 AppCallbacks 构造函数中。有关详细信息,请参阅 UWP 命令行参数

AppCallbacks 函数

功能 描述
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); 将 XAML 控件传递给 Unity,该控件用作 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 线程上调用委托。当您想要从脚本一段代码,允许您创建自己的组件、触发游戏事件、随时间修改组件属性以及以您喜欢的任何方式响应用户输入。更多信息
请参阅术语表
.
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 Unity 开发的脚本后端,在为某些平台构建项目时,可以将其用作 Mono 的替代方案。更多信息
请参阅术语表
脚本后端为Unity中的脚本提供支持的框架。Unity 支持三种不同的脚本后端,具体取决于目标平台:Mono、.NET 和 IL2CPP。但是,通用 Windows 平台仅支持两个:.NET 和 IL2CPP。更多信息
请参阅术语表
以及除图形之外的所有引擎子系统。
bool UnityGetInput() 如果 Unity 处理传入输入,则返回 true。
void UnitySetInput(bool enabled) 启用或禁用输入处理。
bool UnityPause(int pause) 如果传递 1,则暂停 Unity,如果传递 0,则取消暂停。如果您想暂时冻结游戏,此功能非常有用。

其他资源

用于 UWP 的 C# 脚本中的 WinRT API
UWP 的命令行参数