Version: 2023.1
언어: 한국어
데스크톱 플랫폼용 플러그인 빌드
로우레벨 네이티브 플러그인 렌더링 확장 기능

로우레벨 네이티브 플러그인 인터페이스

Unity의 네이티브 플러그인은 특정 이벤트가 발생했을 때 콜백을 받을 수 있습니다. 이를 사용하여 플러그인에서 로우 레벨 렌더링을 구현할 수 있기 때문에 Unity의 멀티스레드 렌더링과 함께 구동할 수 있습니다.

인터페이스 레지스트리

플러그인은 Unity의 메인 이벤트를 처리하는 데 반드시 UnityPluginLoad 함수와 UnityPluginUnload 함수를 익스포트해야 합니다. IUnityInterfaces는 플러그인을 활성화하여 플러그인 API의 IUnityInterface.h에서 찾을 수 있는 다음의 함수에 액세스할 수 있습니다.

# 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>();
}

그래픽스 기기에 액세스

IUnityGraphics.h에 있는 IUnityGraphics 인터페이스를 사용하여 플러그인 액세스를 일반 그래픽스 기기 기능에 제공합니다. 다음의 스크립트는 IUnityGraphics 인터페이스를 사용하여 콜백을 등록하는 방법을 설명합니다.

#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. 
            For example, D3D11 resource creation.
            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;
        }
    };
}

렌더링 스레드에 대한 플러그인 콜백

플랫폼과 사용 가능한 CPU 수가 허용하는 경우 멀티스레딩을 사용하여 Unity에서 렌더링할 수 있습니다.

Note: When you use multithreaded rendering, the rendering API commands happen on a thread that’s completely separate from the thread that runs MonoBehaviour scripts. The communication between the main thread and the render thread means that your plug-in might not start rendering immediately, depending on how much work the main thread has pushed to the render thread.

플러그인에서 렌더링을 하려면 관리되는 플러그인 스크립트에서 GL.IssuePluginEvent를 호출합니다. 이렇게 하면 아래의 코드 예제에 나와 있는 것처럼 Unity의 렌더링 파이프라인이 렌더 스레드에서 네이티브 함수를 호출하게 합니다. 예를 들어 카메라의 OnPostRender 함수에서 GL.IssuePluginEvent를 호출하면 함수는 카메라가 렌더링을 종료한 직후 플러그인 콜백을 호출합니다.

네이티브 플러그인 코드는 다음과 같습니다.

// 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;
}

관리되는 플러그인 코드는 다음과 같습니다.

# 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 in the Native Rendering Plugin sample에서 제공합니다.

OpenGL 그래픽스 API를 사용하는 플러그인

OpenGL 오브젝트에는 다음과 같이 두 종류가 있습니다.

  • OpenGL 컨텍스트간에 제공되는 오브젝트(예: 텍스처, 버퍼, 렌더 버퍼, 샘플러, 쿼리, 셰이더, 프로그램 오브젝트)
  • OpenGL당 컨텍스트 오브젝트(예: 버텍스 배열, 프레임버퍼, 프로그램 파이프라인, 트랜스폼 피드백, 동기화 오브젝트)

Unity uses multiple OpenGL contexts. When initializing and closing the Editor and the Player, Unity relies on a master context, but when rendering it uses dedicated contexts. That is, you can’t create per-context objects during kUnityGfxDeviceEventInitialize and kUnityGfxDeviceEventShutdown events.


데스크톱 플랫폼용 플러그인 빌드
로우레벨 네이티브 플러그인 렌더링 확장 기능
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961