---
title: 原生插件 API 简介
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/introduction
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/introduction.md
generated_at: 2026-11-16T16:31:53.154Z
locale: zh-cn
---

# 原生插件 API 简介

> 找到原生插件 API 文件并将其导入原生插件。

Unity 中的[原生插件](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native.md)在发生某些事件时可以接收回调。您可以使用此功能在插件中实现关卡渲染，以便与 Unity 的多线程渲染配合使用。

## 接口注册表##interface-registry

要手柄主要的 Unity 事件，插件必须导出`UnityPluginLoad`和`UnityPluginUnload`函数。IUnityInterfaces 使插件能够访问这些函数，可在插件 API 中的`IUnityInterface.h`中查找这些函数：

```lang-cpp
#include "IUnityInterface.h"
#include "IUnityGraphics.h"
// Unity plugin load event
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
    UnityPluginLoad(IUnityInterfaces* unityInterfaces)
{
    IUnityGraphics* graphics = unityInterfaces->Get<IUnityGraphics>();
}
```

## 访问图形设备##access-to-the-graphics-device

使用可在 `IUnityGraphics.h` 中查找的 `IUnityGraphics` 接口，允许插件访问 Generic 图形设备功能。此脚本演示了如何使用 `IUnityGraphics` 接口注册回调：

```lang-cpp
#include "IUnityInterface.h"
#include "IUnityGraphics.h"
    
static IUnityInterfaces* s_UnityInterfaces = NULL;
static IUnityGraphics* s_Graphics = NULL;
static UnityGfxRenderer s_RendererType = kUnityGfxRendererNull;
    
// Unity plugin load event
extern "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 event
extern "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 渲染线程的插件回调##plug-in-callbacks-on-the-rendering-thread

如果平台和可用 CPU 数量允许，可以使用多线程在 Unity 中渲染。

> **Note:**
>
> 使用多线程渲染When，渲染 API 命令发生ON与运行 MonoBehaviour 脚本的线程完全分离的线程上。主要线程和渲染线程之间的通信意味着插件可能无法立即Start渲染，具体取决ON线程向渲染线程推送的工作量。

要从插件渲染，请从 Managed 插件脚本调用 [GL.IssuePluginEvent](/engine/6000.0/script-reference/unityengine/gl/issuepluginevent.md)。这会导致 Unity 的渲染管道从渲染线程生函数，如下代码示例所示。例如，如果从 Camera 的 OnPostRender 函数调用 GL.IssuePluginEvent，该函数将在 Camera 完成渲染后立即调用插件回调。

原生插件代码：

```lang-cpp
// Plugin function to handle a specific rendering event
Static void Unity_INTERFACE_API OnRenderEvent(整型 eventID)
{
    // User rendering code
}
    
// Freely defined function to pass a callback to plugin-specific scripts
extern "C" UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
    GetRenderEventFunc()
{
    return OnRenderEvent;
}
```

托管插件代码：

```lang-cs
#if UNITY_IPHONE && !UNITY_EDITOR
[DllImport ("__内部")]
#else
[DllImport("RenderingPlugin")]
#endif
private static extern IntPtr GetRenderEventFunc();
    
// Queue a specific callback to be called on the render thread
GL.IssuePluginEvent(GetRenderEventFunc(), 1);
```

`UnityRenderingEvent`回调的 signature 在原生渲染插件样本中的 [IUnityGraphics.h](https://github.com/Unity-Technologies/NativeRenderingPlugin/tree/master/PluginSource/source/Unity) 中提供。

## 使用 OpenGL 图形 API 的插件##plug-in-using-the-opengl-graphics-api

OpenGL 对象有两种：

* **OpenGL 上下文共享对象**，例如纹理、缓冲区、渲染缓冲区、采样器、查询、着色器和程序对象。
* **Per-OpenGL 上下文对象**，例如 vertex Array、帧缓冲区、程序管道、变换 Feedback 和 Sync 对象。

Unity 使用多个 OpenGL 上下文。初始化和关闭 Editor 和 Player When，Unity 依赖ONMaster下文，但W渲染n渲染使用专用上下文。也就是说，无法在`kUnityGfxDeviceEventInitialize`和`kUnityGfxDeviceEventShutdown`事件期间创建每个上下文对象。

## 使用 Vulkan 图形 API 的插件##plug-in-using-the-vulkan-graphics-api

当开发使用 Vulkan 图形 API 的原生图形插件时，可以通过[验证图层](/engine/6000.0/manual/unity-editor/command-line-arguments/editor.md#debugging)启用调试。要Enable验证图层，请确保以下几点以防止插件执行期间崩溃：

* 在与原生图形插件相同的目录中添加验证图层插件。
* 在 [plug-in Inspector 窗口](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/plug-in-inspector.md)中为原生和 Vulkan 验证图层插件配置相同的 **Load on Startup** Settings。

## 其他资源##additional-resources

* [低关卡原生插件渲染扩展](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-rendering-extensions.md)
* [低关卡原生插件着色器编译器访问](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-shader-compiler-access.md)
* [低关卡原生插件内存管理员 API](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-memory-manager-api.md)
* [低关卡原生插件内存管理员 API 参考](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-memory-manager-api-reference.md)
* [ON Android Vulkan 验证图层](https://developer.android.com/ndk/guides/graphics/validation-layer)
