XR 提供程序是 Unity 项目的一部分,由至少一个清单文件和一个或多个原生插件组成。它还可以包括其他资源,例如脚本和图像。只要启动编辑器时您的项目包含这些内容,Unity 就会发现它们。
注意:每当您更改提供程序的清单或编辑器原生插件时,都必须重新启动 Unity。
原生插件必须位于相对于 UnitySubsystemsManifest.json
的子文件夹中。如果您的提供程序不是包的一部分,Unity 只能在 Assets
文件夹中最多向下一层找到 UnitySubsystemsManifest.json
。
此清单包含有关您的提供程序的信息,例如它提供的子系统和插件名称。
有关更多信息,请参阅 UnitySubsystemsManifest.json 页面。
要了解如何为目标平台构建原生插件,请参阅有关 Unity 原生插件接口 的文档。将您的动态库导入 Unity 后,请确保所有选项(如插件设置中的目标平台)是正确的。
您需要为要实现的子系统注册一个生命周期处理程序。例如:
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
UnityPluginLoad(IUnityInterfaces* unityInterfaces)
{
s_XrDisplay = unityInterfaces->Get<IUnityXRDisplayInterface>();
UnityLifecycleProvider displayLifecycleHandler =
{
NULL, // 这可以是要以 userData 形式传递给以下函数的任何对象
&Lifecycle_Initialize,
&Lifecycle_Start,
&Lifecycle_Stop,
&Lifecycle_Shutdown
};
s_XrDisplay->RegisterLifecycleProvider("Provider Plugin Name", "Display0", &displayLifecycleHandler);
// 注册其他子系统
}
注意:传递给 RegisterLifecycleProvider
的参数必须与您清单文件中的 name
和 id
字段匹配。
当您稍后调用 Initialize
方法时,将获得一个实例句柄,您可以使用它来调用接受 UnitySubsystemHandle
的方法。示例:
/// 在子系统应初始化以准备进入活动状态时执行的回调。
static UnitySubsystemErrorCode UNITY_INTERFACE_API Lifecycle_Initialize(UnitySubsystemHandle handle, void* data)
{
// 在图形线程上注册回调。
UnityXRDisplayGraphicsThreadProvider gfxThreadProvider = { NULL, NULL, &GfxThread_WaitForNextFrameDesc, NULL };
s_XrDisplay->RegisterProviderForGraphicsThread(handle, &gfxThreadProvider);
return kUnitySubsystemErrorCodeSuccess;
}
SDK 包中包含一个构建示例插件的示例项目。
有关在 Unity 中加载提供程序的更多信息,请参阅子系统的运行时发现和激活页面。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.