デスクトップ プラットフォームのプラグインをビルド
テキスト形式シーンファイルのフォーマット

低レベル ネイティブ プラグイン インターフェース

基本的なスクリプトのインターフェースに加えて,ネイティブコードプラグイン(Native Code Plugins) はUnity上で発生したの特定のイベントをコールバックで受け取ることができます。これは主に,プラグインで低レベルのレンダリングを実装し,Unityのマルチスレッドレンダリングを操作できるようにするために使用されます。

グラフィックドライバへのアクセス

プラグインは UnitySetGraphicsDevice 関数をエクスポートすることにより,グラフィックドライバの通知を受け取る事ができます。これはグラフィックデバイスが作成される時,そして破棄される前,またデバイスを“リセット”する前と後(これはDirect3D 9のみ発生)に呼び出されます。この関数のパラメータは,デバイスのポインタや,デバイスのパラメータ,デバイスで行われているイベントの種類を持っています。

// If exported by a plugin, this function will be called when graphics device is created, destroyed,
// and before and after it is reset (ie, resolution changed).
extern "C" void EXPORT_API __UnitySetGraphicsDevice__ (void* device, int deviceType, int eventType);

deviceTypeに指定可能な値:

enum GfxDeviceRenderer {
    kGfxRendererOpenGL = 0, // OpenGL
    kGfxRendererD3D9 = 1, // Direct3D 9
    kGfxRendererD3D11 = 2, // Direct3D 11
    kGfxRendererGCM = 3, // Sony PlayStation 3 GCM
    kGfxRendererNull = 4, // "null" device (used in batch mode)
    kGfxRendererHollywood = 5, // Nintendo Wii
    kGfxRendererXenon = 6, // Xbox 360
    kGfxRendererOpenGLES = 7, // OpenGL ES 1.1
    kGfxRendererOpenGLES20Mobile = 8, // OpenGL ES 2.0 mobile variant
    kGfxRendererMolehill = 9, // Flash 11 Stage3D
    kGfxRendererOpenGLES20Desktop = 10, // OpenGL ES 2.0 desktop variant
};

eventTypeに指定可能な値:

enum GfxDeviceEventType {
    kGfxDeviceEventInitialize = 0,
    kGfxDeviceEventShutdown = 1,
    kGfxDeviceEventBeforeReset = 2,
    kGfxDeviceEventAfterReset = 3,
};

レンダリングスレッド上のプラグインコールバック

Unity は当該プラットフォームでの CPU 数により実現できる範囲でレンダリングをマルチスレッド化することができます。マルチスレッドレンダリングを使う時,レンダリングAPIコマンドはMonoBehaviourとは完全に別の,独立したスレッド上で実行します。そのため,いつでもプラグインからレンダリングを起動することが可能とは限りません。そして,レンダリングスレッドは干渉する可能性があります。

プラグインから どのような レンダリングを行う場合でも,プラグインはスクリプトから GL.IssuePluginEvent を参照しメインスレッドから呼び出されるようにする必要があります。例えば,カメラのOnPostRender関数から GL.IssuePluginEventを呼び出す場合,プラグインはカメラのレンダリング終了後すぐにコールバックを取得します。

// If exported by a plugin, this function will be called for GL.IssuePluginEvent script calls.
// The function will be called on a rendering thread; note that when multithreaded rendering is used,
// the render thread WILL BE DIFFERENT from the main thread, on which all scripts & other game logic are executed!
// You have responsibility for ensuring any necessary synchronization with other plugin script calls takes place.
extern "C" void EXPORT_API __UnityRenderEvent__ (int eventID);

サンプル

低レベルレンダリングプラグインのサンプルを2つ紹介します。 ここでダウンロード出来ます

  • すべての正規レンダリングが完了した後に,C++コードから回転する三角形を描画する
  • Texture.GetNativeTexturePtrを利用してアクセスし,C++のコードから手続き型テクスチャ(Procedural texture)を塗りつぶす

プロジェクトは以下で動作します:

  • プロジェクトはWindows(Visual Studio 2008)及びMacOSX(Xcode 3.2)で動作し,Direct3D 9,Direct3D 11,OpenGLのいずれかで使用しています。Direct3D 9のコードは,“滅びた”デバイスを処理する方法を示しています。
  • Windows Store Apps。詳細はRenderingPlugin\WSAVisualStudio2012を御覧ください
デスクトップ プラットフォームのプラグインをビルド
テキスト形式シーンファイルのフォーマット