I am trying to load raw image data into a Texture2D in Unity using
LoadRawTextureData but I can't get it to work correctly.
First I receive an array of bytes from an unmanaged function that captures a window. I am pretty sure these bytes represent an image in BGRA32 format.
int dataSize = width * height * 4; byte imgData = new byte[dataSize]; bool success = CaptureWindow(windowInfo, imgData, (uint)(dataSize));
Then I try to create a new Texture2D with this data
Texture2D tex = new Texture2D(width, height, TextureFormat.BGRA32, false, true); tex.LoadRawTextureData(imgData); tex.Apply();
And finally I create a new plane and assign this texture to its material.
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); plane.GetComponent<MeshRenderer>().material.mainTexture = tex;
However, when I reverse the byte array before loading it into the texture as ARGB32, it does work:
Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false, true); Array.Reverse(imgData); tex.LoadRawTextureData(imgData); tex.Apply();
The problem with this solution is that
Array.Reverse is too slow.
Why doesn't loading it as BGRA32 work? Isn't BGRA32 just the reverse of ARGB32?
FYI: I am using Unity 5.1.2f1 on Windows and
SystemInfo.SupportsTextureFormat returns true for both BGRA32 and ARGB32.