Convert texture from Compressed to TrueColor in scripts.


I’m having a problem with the format for my textures, you see, I am making a system that gets a texture and based on the color it will create different tiles (so each Color32 value is assigned to a tile).

My problem is that unity’s file compression makes some of the colors slightly different, if I keep one of my file as compressed in the import settings. Some of the (128, 128, 128, 255) colors will be changed to (129, 128, 129, 255). So I thought that by changing the texture’s format it would work. And when I changed it to true color in the import settings, everything was fine. But thing is, I plan on making a system so those textures can be changed/imported by the player/user, and they won’t be able to change the texture’s import settings, so I need a way to convert my textures to true color through scripts.

TLDR: I can’t convert my texture’s format from DXT1 (sometimes DXT5) to RGB24 (sometimes RGBA32). Because I get this error: UnityException: LoadRawTextureData: not enough data provided (will result in overread).

Anyways, here’s my code:

 Texture2D tex = LoadAsset("levels/"+name) as Texture2D;
                // Creates a copy of the texture so I'm able to manipulate it without having to set it to readable in UNITY (will be useful later when resource packs will be added)
                var texture = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, tex.mipmapCount > 1);
                texture.alphaIsTransparency = true;
                texture.filterMode = FilterMode.Point;


You can copy pixels between different textures with different texture formats (but with same number of pixels) like this:

var myPixels = srcTexture.GetPixels32();

There’s also this method from Unity 5.6 and up: