/TextureUpdateExample

An example showing how to update textures from a native plugin in Unity.

Primary LanguageC

TextureUpdateExample

gif

Old-school plasma effect generated by C++ code

This is an example that shows how to use the CustomTextureUpdate callback that allows native plugins to update contents of textures in a thread safe and platform agnostic way.

How to implement the CustomTextureUpdate callback

The callback function should be implemented with the following signature:

void TextureUpdateCallback(int eventID, void* data)
{
  auto event = (UnityRenderingExtEventType)eventID;
  auto params = (UnityRenderingExtTextureUpdateParamsV2*)data;
}

The type of the event will be given to eventID, and the attributes of the target texture will be given with UnityRenderingExtTextureUpdateParamsV2 struct carried by the data pointer.

The possible values of eventID are defined in UnityRenderingExtEventType; Only kUnityRenderingExtEventUpdateTextureBeginV2 and kUnityRenderingExtEventUpdateTextureEndV2 are relevant to the texture update callback.

kUnityRenderingExtEventUpdateTextureBeginV2 event

This event is invoked right before updating the texture. You can give raw image data via the texData pointer in the parameter struct.

if (event == kUnityRenderingExtEventUpdateTextureBeginV2)
{
  uint8_t* img = new uint32_t[params->width * params->height * 4];

  // Set image data here.

  params->texData = img;
}

You can also give nullptr to texData when you don't like to update the texture in this frame.

kUnityRenderingExtEventUpdateTextureEndV2 event

This event is invoked right after updating the texture. You can safely release resources used to update the texture.

if (event == kUnityRenderingExtEventUpdateTextureEndV2)
{
  delete[] reinterpret_cast<uint32_t*>(params->texData);
}

Interface function

You have to implement an interface function that is used to retrieve the pointer of the callback function.

extern "C"
UnityRenderingEventAndData UNITY_INTERFACE_EXPORT GetTextureUpdateCallback()
{
    return TextureUpdateCallback;
}

For further details of the plugin implementation, please see the example source code contained in this repository.

How to update texture from C# script

In order to request texture update from a C# script, you can use IssuePluginCustomTextureUpdateV2 with a CommandBuffer. The pointer to the callback function and a reference to a texture object should be given to the command. You can also give a single uint value to the command that can be used as user data to the callback.

[DllImport("DllName")] static extern IntPtr GetTextureUpdateCallback();

var callback = GetTextureUpdateCallback();
m_CommandBuffer.IssuePluginCustomTextureUpdateV2(callback, texture, userData);
Graphics.ExecuteCommandBuffer(m_CommandBuffer);

Platform availability

At the moment of Unity 2018.3, the CustomTextureUpdate callback is only available in Direct3D 11, Metal, OpenGL (Core/ES), and Nintendo Switch. It's also available on Vulkan from Unity 2019.1. For other platforms, you have to implement the plugin without using this interface.