Re-save TGA to disk

I have some code that loads a TGA, like so:

AssetDatabase.ImportAsset("Assets/blah.tga", ImportAssetOptions.ForceSynchronousImport);
            Texture2D textureMem = (Texture2D)Resources.LoadAssetAtPath("Assets/blah.tga", typeof(Texture2D));

I alter the texture using Texture2D.SetPixels and Texture2D.Apply as described here:

So, I want to save out the altered texture back to “Assets/blah.tga”. I’ve tried AssetDatabase.SaveAssets and have also tried to figure out how to get to the raw bytes of the TGA, but all I see available is Texture2D.EncodeToPNG - I don’t want to change the format away from TGA. Anyone know how to handle this?

Write a TGA exporter; the only built-in encoding Unity has, as you discovered, is PNG.

–Eric

Hi Eric,

Can you elaborate please? Are you saying that I should just be writing a function to write the raw bytes in a TGA format? Or are you saying that there is a special way to integrate a “TGA Exporter” into Unity?

Thanks.

Yep. I wrote a TIFF exporter a while ago (a rather archaic format I must say).

It’s possible there’s a TGA exporting .net assembly out there, but a brief search didn’t turn up anything; I’m not sure the format is used enough to warrant that.

–Eric