原生插件 API 简介
找到原生插件 API 文件并将其导入原生插件。
阅读时间5 分钟最后更新于 6 天前
Unity 中的原生插件在发生某些事件时可以接收回调。您可以使用此功能在插件中实现关卡渲染,以便与 Unity 的多线程渲染配合使用。
接口注册表
要手柄主要的 Unity 事件,插件必须导出和函数。IUnityInterfaces 使插件能够访问这些函数,可在插件 API 中的中查找这些函数:
UnityPluginLoadUnityPluginUnloadIUnityInterface.h#include "IUnityInterface.h"#include "IUnityGraphics.h"// Unity plugin load eventextern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces){ IUnityGraphics* graphics = unityInterfaces->Get<IUnityGraphics>();}
访问图形设备
使用可在 中查找的 接口,允许插件访问 Generic 图形设备功能。此脚本演示了如何使用 接口注册回调:
IUnityGraphics.hIUnityGraphicsIUnityGraphics#include "IUnityInterface.h"#include "IUnityGraphics.h" static IUnityInterfaces* s_UnityInterfaces = NULL;static IUnityGraphics* s_Graphics = NULL;static UnityGfxRenderer s_RendererType = kUnityGfxRendererNull; // Unity plugin load eventextern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces){ s_UnityInterfaces = unityInterfaces; s_Graphics = unityInterfaces->Get<IUnityGraphics>(); s_Graphics->RegisterDeviceEventCallback(OnGraphicsDeviceEvent); // Run OnGraphicsDeviceEvent(initialize) manually on plugin load // to not miss the event in case the graphics device is already initialized OnGraphicsDeviceEvent(kUnityGfxDeviceEventInitialize);} // Unity plugin unload eventextern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload(){ s_Graphics->UnregisterDeviceEventCallback(OnGraphicsDeviceEvent);} static void UNITY_INTERFACE_API OnGraphicsDeviceEvent(UnityGfxDeviceEventType eventType){ switch (eventType) { case kUnityGfxDeviceEventInitialize: { s_RendererType = s_Graphics->GetRenderer(); //TODO: user initialization code on graphics device initialization. 例如,D3D11 Resource创建。 break; } case kUnityGfxDeviceEventShutdown: { s_RendererType = kUnityGfxRendererNull; //TODO: user graphics API code to call on graphics device shutdown. break; } case kUnityGfxDeviceEventBeforeReset: { //TODO: user graphics API code to call before graphics device reset. break; } case kUnityGfxDeviceEventAfterReset: { //TODO: user graphics API code to call after graphics device reset. break; } };}
ON 渲染线程的插件回调
如果平台和可用 CPU 数量允许,可以使用多线程在 Unity 中渲染。
要从插件渲染,请从 Managed 插件脚本调用 GL.IssuePluginEvent。这会导致 Unity 的渲染管道从渲染线程生函数,如下代码示例所示。例如,如果从 Camera 的 OnPostRender 函数调用 GL.IssuePluginEvent,该函数将在 Camera 完成渲染后立即调用插件回调。
原生插件代码:
// Plugin function to handle a specific rendering eventStatic void Unity_INTERFACE_API OnRenderEvent(整型 eventID){ // User rendering code} // Freely defined function to pass a callback to plugin-specific scriptsextern "C" UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetRenderEventFunc(){ return OnRenderEvent;}
托管插件代码:
#if UNITY_IPHONE && !UNITY_EDITOR[DllImport ("__内部")]#else[DllImport("RenderingPlugin")]#endifprivate static extern IntPtr GetRenderEventFunc(); // Queue a specific callback to be called on the render threadGL.IssuePluginEvent(GetRenderEventFunc(), 1);
UnityRenderingEvent使用 OpenGL 图形 API 的插件
OpenGL 对象有两种:
- OpenGL 上下文共享对象,例如纹理、缓冲区、渲染缓冲区、采样器、查询、着色器和程序对象。
- Per-OpenGL 上下文对象,例如 vertex Array、帧缓冲区、程序管道、变换 Feedback 和 Sync 对象。
Unity 使用多个 OpenGL 上下文。初始化和关闭 Editor 和 Player When,Unity 依赖ONMaster下文,但W渲染n渲染使用专用上下文。也就是说,无法在和事件期间创建每个上下文对象。
kUnityGfxDeviceEventInitializekUnityGfxDeviceEventShutdown使用 Vulkan 图形 API 的插件
当开发使用 Vulkan 图形 API 的原生图形插件时,可以通过验证图层启用调试。要Enable验证图层,请确保以下几点以防止插件执行期间崩溃:
- 在与原生图形插件相同的目录中添加验证图层插件。
- 在 plug-in Inspector 窗口中为原生和 Vulkan 验证图层插件配置相同的 Load on Startup Settings。