I am loading PNG files for image processing and using Texture2D.GetRawTextureData to read/write the pixels efficiently. Working on many 4K maps and have identified this as the faster way of editing them.
That’s great, except I ran into an issue when loading PNG files… they load as ARGB32 by default, and GetRawTextureData has the color channels out of order due to the different format.
I can convert the texture immediately after loading it like this…
tex.LoadImage(File.ReadAllBytes(fullPath));
if (tex.format != TextureFormat.RGBA32)
{
var convertedTex = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false);
convertedTex.SetPixels32(tex.GetPixels32());
tex = convertedTex;
}
which is fine and it works, but I profiled it and the time it takes is not insignificant.
Anyhoo, is there a more efficient way of loading PNG files directly to RGBA32, or more efficient way of converting? I tried using Graphics.ConvertTexture, but it did not succeed. I assume it is because RGBA32 is not listed in as a RenderTextureFormat ( as per the documentation for Graphics.ConvertTexture ) GetRawTextureData instead, but it would be nice to get the raw texture data in a color struct.
As I write this, I realized that I can create my own ARGB32 struct and use it like such…
public struct ColorARGB32
{
public byte a;
public byte r;
public byte g;
public byte b;
}
...
var pixels = GetRawTextureData<ColorARGB32>();
which should do the trick. However, I’ll post this anyway in case y’all have a better idea, or in case someone else can use this info.