---
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/ja-jp/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/introduction
latest_url: https://docs.unity.com/ja-jp/engine/7000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/introduction.md
generated_at: 2026-11-16T09:54:09.077Z
locale: ja-jp
---

# ネイティブ プラグイン 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` インターフェースを使用して、プラグインにジェネリックグラフィックスデバイス機能へのアクセスを提供します。このスクリプトは、`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 リソース作成など。
            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;
        }
    };
}
```

## レンダリングスレッドのプラグインコールバック##plug-in-callbacks-on-the-rendering-thread

プラットフォームと使用可能な CPU 数が許す限り、マルチスレッドを使用して Unity でレンダリングすることができます。

> **Note:**
>
> マルチスレッドレンダリングを使用する場合、レンダリング API コマンドは MonoBehaviour スクリプトを実行するスレッドとは完全に別のスレッドで実行されます。メインスレッドとレンダリングするスレッド間の通信は、メインスレッドがレンダリングするスレッドにプッシュした作業量に応じて、プラグインがすぐにレンダリングを開始しないことを意味します。

プラグインからレンダリングするには、マネージプラグインスクリプトから [GL.IssuePluginEvent](/engine/6000.0/script-reference/unityengine/gl/issuepluginevent.md) を呼び出します。これにより、以下のコード例に示すように、Unity のレンダーパイプラインがレンダリングするスレッドからネイティブ機能をコールします。例えば、カメラの OnPostRender 関数から GL.IssuePluginEvent をコールすると、カメラのレンダリングが完了した直後にプラグインコールバックが関数からコールされます。

ネイティブプラグインコード：

```lang-cpp
// Plugin function to handle a specific rendering event
static void UNITY_INTERFACE_API OnRenderEvent(int 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 ("__Internal")]
#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` コールバックのシグネチャは、[IUnityGraphics.h のネイティブレンダリングプラグインサンプル](https://github.com/Unity-Technologies/NativeRenderingPlugin/tree/master/PluginSource/source/Unity)で提供されています。

## OpenGL グラフィックス API を使用したプラグイン##plug-in-using-the-opengl-graphics-api

OpenGL オブジェクトには 2 種類あります。

* テクスチャ、バッファ、レンダーバッファ、サンプラー、クエリ、シェーダー、プログラムオブジェクトなど、**OpenGL コンテキストで共有**されるオブジェクト。
* 頂点配列、フレームバッファ、プログラムパイプライン、Transform フィードバック、同期オブジェクトなど、**OpenGL ごとの**コンテキストオブジェクト。

Unity は複数の OpenGL コンテキストを使用します。エディターとプレイヤーの初期化と終了時には、Unity はマスターコンテキストに依存しますが、レンダリング時には専用のコンテキストを使用します。つまり、`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)を通じてデバッグを有効にできます。検証レイヤーを有効にするには、プラグイン実行中のクラッシュを防ぐために以下の確認してください。

* ネイティブグラフィックスプラグインと同じディレクトリに検証レイヤープラグインを追加します。
* [Plugin Inspector ウィンドウ](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/plug-in-inspector.md)で、ネイティブと Vulkan 検証レイヤープラグインの両方に同じ**起動時に**ロード設定を行います。

## 追加リソース##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)
* [AndroidのVulkan検証レイヤー](https://developer.android.com/ndk/guides/graphics/validation-layer)
