此接口用于在创建图形设备之前对其进行配置。常见用例包括在多 GPU 场景中选择特定 GPU、启用图形扩展和修改缓冲区创建。
您的提供程序可以导出原生符号 XRSDKPreInit
,它在运行时初始化的早期,在图形设备创建之前被调用。它应将相关的入口点填入 UnityXRPreInitProvider
结构,以便为引擎提供在早期初始化期间可能需要的信息。某些提供程序需要这些信息,因为图形设备的创建早于提供程序初始化。
Unity 项目构建过程需要您的提供程序库的名称才能在引擎启动时调用 XRSDKPreInit()
。为此,需要在您的 XRLoader
上实现 IXRLoaderPreInit.GetPreInitLibraryName
接口:
public string GetPreInitLibraryName(BuildTarget buildTarget, BuildTargetGroup buildTargetGroup)
{
return "XRSDKMyProviderLibraryName";
}
当 Unity 启动时,它会运行上面的代码来加载库。然后,Unity 打开该库,查找 XRSDKPreInit
导出并调用它。此时,XRSDKPreInit
应该注册一个 UnityXRPreInitProvider
。
这是一个简单的示例,它注册了您可以用于早期引擎初始化的所有回调:
XRSDKPreInit(IUnityInterfaces * interfaces)
{
IUnityXRPreInit* preInit = (IUnityXRPreInit*)interfaces->GetInterface(GetUnityInterfaceGUID<IUnityXRPreInit>());
UnityXRPreInitProvider provider = { 0 };
provider.userData = nullptr;
provider.GetPreInitFlags = GetPreInitFlags;
provider.GetGraphicsAdapterId = GetGraphicsAdapterId;
provider.GetVulkanInstanceExtensions = GetVulkanInstanceExtensions;
provider.GetVulkanDeviceExtensions = GetVulkanDeviceExtensions;
preInit->RegisterPreInitProvider(&provider);
}
将不需要的任何回调设置为 nullptr
。
GetPreInitFlags
返回一个 64 位的标志位域。目前公开了以下标志:
kUnityXRPreInitFlagsEGLUsePBuffer
- 在 EGL 平台(Android、独立 Oculus 设备等)上,EGL 应该使用 PBuffers 进行初始化。kUnityXRPreInitFlagsEGLUseNoErrorContext
- 在 EGL 平台上,EGL 应该用 NO_ERROR 上下文。提供程序不应设置任何未定义的位,因为它们可能用于未来的扩展。
GetGraphicsAdapterId
设置图形适配器,以供 Unity 运行时图形设备用于图形 API 初始化。例如,如果用户将头盔插入不同于用户主显示设备的 GPU,Unity 可能需要明确针对次要 GPU 初始化图形 API。
如果提供程序需要 Unity 来初始化具有设备和实例特定扩展的 Vulkan,则使用 GetVulkanDeviceExtensions
和 GetVulkanInstanceExtensions
。这通常用于合成器表面共享。二者都返回一个以空格分隔的扩展字符串。
GetPreInitLibraryName
仅在 XRManager 预制件中的第一个 XRLoader 上调用,因此只有列表中的第一个提供程序可以在运行时提供 PreInit 入口点。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.