- Home /
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++;
}
Hi! Thanks for sharing this. I tried to implement this in UnityScript. I translated everything but it says: "$$anonymous$$ identifier: 'FillInClear'." I tried searching the net for that "FillInClear" but nothing came up. Could you please tell me what is it and what does it do?
Hi there. FillInClear isn't a unity feature. It's a function I wrote that's not in this script. So just tak,e out that line, and you're good.
All it did was fill in transparent pixels with white.
Forgot to say thank you for FillInClear explanation, it was a busy day, so... thank you :)
So... this code in the question is actualy working? or is the one from before you solve it?
Answer by Catlard · Oct 14, 2012 at 02:29 PM
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!
Sure! I'll post the full script that I used here:
using UnityEngine; using System.Collections; using System.IO; using System;
public class SaveTextureToDesktop : Singleton$$anonymous$$onoBehaviour {
public Camera _cameraWithRendTexture;
public Text$$anonymous$$esh _percent$$anonymous$$esh;
public Text$$anonymous$$esh _name$$anonymous$$esh;
public int _heightInPixelsOfCertificate = 1488;
public void SaveCertificate() {
if(!_cameraWithRendTexture)
_cameraWithRendTexture = GameObject.Find("CertificateCamera").GetComponent<Camera>();
if(!_name$$anonymous$$esh)
_name$$anonymous$$esh = GameObject.Find("bName").GetComponent<Text$$anonymous$$esh>();
if(!_percent$$anonymous$$esh)
_percent$$anonymous$$esh = GameObject.Find ("dPercent").GetComponent<Text$$anonymous$$esh>();
RenderTexture.active = _cameraWithRendTexture.targetTexture;
Texture2D newTexture = new Texture2D(_cameraWithRendTexture.targetTexture.width, _cameraWithRendTexture.targetTexture.height, TextureFormat.ARGB32, false);
newTexture.ReadPixels(new Rect(0,0,_cameraWithRendTexture.targetTexture.width, _cameraWithRendTexture.targetTexture.height), 0,0,false);
newTexture.Apply();
Color[] colorArrayCropped = newTexture.GetPixels(0,newTexture.height-_heightInPixelsOfCertificate, newTexture.width, _heightInPixelsOfCertificate);
Texture2D croppedTex = new Texture2D(newTexture.width,_heightInPixelsOfCertificate, TextureFormat.ARGB32, false);
croppedTex.SetPixels(colorArrayCropped);
croppedTex = FillInClear(croppedTex, _cameraWithRendTexture.backgroundColor);
croppedTex.Apply();
byte[] bytes = croppedTex.EncodeToPNG();
File.WriteAllBytes(PathBuilder.instance.GetFilePath(FileType.Certificate) + _name$$anonymous$$esh.text +"'s certificate (" +_percent$$anonymous$$esh.text + ")" +".png", bytes);
}
public Texture2D FillInClear(Texture2D tex2D, Color whatToFillWith) {
for(int i = 0; i < tex2D.width; i++) {
for(int j = 0; j < tex2D.height; j++) {
if(tex2D.GetPixel(i,j) == Color.clear)
tex2D.SetPixel(i,j, whatToFillWith);
}
}
return tex2D;
}
}
Thanks :) but do you have a free $$anonymous$$ute to check out my problem? Can you help me :) Thanks http://answers.unity3d.com/questions/531887/encodetopng-and-webcamtexture-problem.html
Answer by SunzWong · Jun 26, 2015 at 07:56 AM
can i ask what are these for? if(!_nameMesh) nameMesh = GameObject.Find("bName").GetComponent(); if(!_percentMesh) percentMesh = GameObject.Find ("dPercent").GetComponent();
if( !_name$$anonymous$$esh ) { }
just means:
if( _name$$anonymous$$esh==null ) { }
Answer by jemonsuarez · Aug 27, 2016 at 05:39 AM
If anybody finds this useful, this asset does it all and includes a lot of other features:
Your answer
Follow this Question
Related Questions
Save rendertexture to image in file 2 Answers
EncodeToPng and WebCamTexture PROBLEM 4 Answers
Wait for Texture.ReadPixels 1 Answer
ReadPixels requires an offset while in the Editor? 2 Answers
Issue With Simultaneous ReadPixels Calls 0 Answers