Saving a Texture2D as a PNG

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++;
	}

AVAST! I have figured it out, and edited the script above, to reflect my solution. Thank ye, gigantic teddy bear of the internet to whom I tell all my problems!

can i ask what are these for?
if(!_nameMesh)
nameMesh = GameObject.Find(“bName”).GetComponent();
if(!_percentMesh)
percentMesh = GameObject.Find (“dPercent”).GetComponent();

If anybody finds this useful, this asset does it all and includes a lot of other features:

File Management: Easy way to save and read files.