What's the proper way to update a texture every frame?

Kinect gives 30 color frames every second, and it's in a byte[ ] array, right now i have to convert it to Color32, then use Texture2D.SetPixels32(colorData) and Texture2D.Apply() to display it in the scene.

this is extremely slow, because of the convertion and data copying. what may help?

What you are doing is the standard proper unity way.

Another way is via binding the texture using the native pointer but requires -force-opengl I think. Check the source here


It's in this section of the code. Not sure if this is still 4.6 compatible.

void WriteImageToTexture(Texture2D tex)
// NOTE: This method only works when unity is rendering with OpenGL ("unity.exe -force-opengl"). This is *much* faster
// then Texture2D::SetPixels32 which we would have to use otherwise
// NOTE: The native texture id needs a +1 if we are rendering to GUI
if (openGl) {
//Gl.glBindTexture(Gl.GL_TEXTURE_2D, (null == target) ? tex.GetNativeTextureID() + 1 : tex.GetNativeTextureID());
//Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, 0, 0, (int)inputSize.x, (int)inputSize.y, Gl.GL_RGB, Gl.GL_UNSIGNED_BYTE, Image.ImageMapPtr);
GL.BindTexture(GL.TEXTURE_2D, (null == target) ? tex.GetNativeTextureID() + 1 : tex.GetNativeTextureID());
GL.TexSubImage2D(GL.TEXTURE_2D, 0, 0, 0, (int)inputSize.x, (int)inputSize.y, GL.RGB, GL.UNSIGNED_BYTE, Image.ImageMapPtr);

Not sure if BindTexture and TextSubImage2D exist :(.... I heard you can maybe GL.IssuePluginEvent(texture_id) and do some processing in a native plugin.

I use Marshal.Copy except a for-loop, then the performance is improved.