SOLVED 5 MINUTES AFTER ASKING, BY SHEER FORCE OF WILL. AND FOR REMEMBERING GETPIXELS.
Hey, folks!
So, I’ve got this function. It attempts to save a Texture2D from a material’s mainTexture property, so it can be saved to the desktop. I can’t use readpixels, because the size of the texture is actually larger than the screen. But when I attempt to either change the format of a current texture2D so it can be encoded, I run into problems. Specifically, I’m getting this problem: Assets/Scripts/Utils/SaveTextureToDesktop.cs(16,30): error CS0200: Property or indexer `UnityEngine.Texture2D.format’ cannot be assigned to (it is read only) My function is below. Is there something I’m missing on how to do this?
public void Save() {
Texture2D savedTexture = _materialToSave.mainTexture as Texture2D;
Texture2D newTexture = new Texture2D(savedTexture.width, savedTexture.height, TextureFormat.ARGB32, false);
newTexture.SetPixels(0,0, savedTexture.width, savedTexture.height, savedTexture.GetPixels());
newTexture = FillInClear(newTexture);
newTexture.Apply();
byte[] bytes = newTexture.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/../testscreen-" + imageCount + ".png", bytes);
//Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
imageCount++;
}