Integration with Unity

Diligent Engine supports integration with Unity through Unity low-level native plugin interface. The engine relies on Native API Interoperability to attach to the graphics API initialized by Unity. After Diligent Engine device and context are created, they can be used us usual to create resources and issue rendering commands. Few API-specific steps need to be performed to ensure correct communication with Unity.


Unity issues kUnityGfxDeviceEventInitialize event to let the plugin initialize its rendering resources. The following code snippet shows how Diligent Engine can be attached to D3D11 device returned by Unity:

After the plugin completed with the rendering commands, it needs to call IRenderDevice::InvalidateState() to clear internal state cache.



To attach Diligent Engine to existing D3D12 device, it is necessary to implement  ICommandQueueD3D12 interface (see Direct3D12 Interoperability for details). The following code shows how this interface can be implemented with the help of IUnityGraphicsD3D12v2 interface provided by Unity:

When Unity issues  kUnityGfxDeviceEventInitialize event,   IEngineFactoryD3D12::AttachToD3D12Device() method can be used to initialize Diligent Engine as shown below:

After the plugin completed with the rendering commands, it is necessary to explicitly flush the context to submit all commands for execution, invalidated context state and call IRenderDeviceD3D12::FinishFrame() to let the device perform necessary operations:



To attach Diligent Engine to an existing OpenGL context, call  IEngineFactoryOpenGL::AttachToActiveGLContext():

Unity uses multiple GL contexts. To update active GL context in Diligent Engine, it is necessary to call IDeviceContextGL::UpdateCurrentGLContext() before issuing any other command:

Similar to other APIs, it is necessary to call  IRenderDevice::InvalidateState() when all commands are issued to clear OpenGL state cache: