- Home /
Is it possible to save RenderTextures into png files for later use?
Hi,
Is it possible to save RenderTextures into png files for later use?
try this one for essentially the code needed ...
http://answers.unity3d.com/questions/227280/create-a-texture2d-dynamically-from-a-screenshot-o.html
Answer by Eric5h5 · Dec 07, 2010 at 06:34 PM
Use ReadPixels to read the RenderTexture into a Texture2D and save that using EncodeToPNG.
Thanks for your help. I managed to capture a render texture, but it seems that it is only possible in OnRenderImage(). Is it possible to do it in other methods?
@AliAzin: you should be able to do it with Camera.Render.
here's some random example code, that may be of help to someone.
Starting at "RenderTexture tempRT = ..."
public void $$anonymous$$akeSquarePngFromOurVirtualThingy()
{
// capture the virtuCam and save it as a square PNG.
int sqr = 512;
virtuCamera.camera.aspect = 1.0f;
// recall that the height is now the "actual" size from now on
RenderTexture tempRT = new RenderTexture(sqr,sqr, 24 );
// the 24 can be 0,16,24, formats like
// RenderTextureFormat.Default, ARGB32 etc.
virtuCamera.camera.targetTexture = tempRT;
virtuCamera.camera.Render();
RenderTexture.active = tempRT;
Texture2D virtualPhoto =
new Texture2D(sqr,sqr, TextureFormat.RGB24, false);
// false, meaning no need for mipmaps
virtualPhoto.ReadPixels( new Rect(0, 0, sqr,sqr), 0, 0);
RenderTexture.active = null; //can help avoid errors
virtuCamera.camera.targetTexture = null;
// consider ... Destroy(tempRT);
byte[] bytes;
bytes = virtualPhoto.EncodeToPNG();
System.IO.File.WriteAllBytes(
OurTempSquareImageLocation(), bytes );
// virtualCam.SetActive(false); ... no great need for this.
// now use the image,
UseFileImageAt( OurTempSquareImageLocation() );
}
private string OurTempSquareImageLocation()
{
string r = Application.persistentDataPath + "/p.png";
return r;
}
UseFileImageAt( OurTempSquareImageLocation() ); //what is this? to what I supposed to change?
Answer by Statement · Dec 07, 2010 at 02:53 PM
Interesting question.
You can grab contents of the current render target with Texture2D.ReadPixels
Texture2Ds can generate png data with Texture2D.EncodeToPNG.
Answer by unity_P_rZF6EevSyxyg · Dec 14, 2018 at 01:12 PM
public RenderTexture rt;
// Use this for initialization
public void SaveTexture () {
byte[] bytes = toTexture2D(rt).EncodeToPNG();
System.IO.File.WriteAllBytes("C:/Users/egsha/SavedScreen.png", bytes);
}
Texture2D toTexture2D(RenderTexture rTex)
{
Texture2D tex = new Texture2D(1920, 1080, TextureFormat.RGB24, false);
RenderTexture.active = rTex;
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
tex.Apply();
return tex;
}
don't to forget dispose Texture2D
public RenderTexture rt;
// Use this for initialization
public void SaveTexture () {
byte[] bytes = toTexture2D(rt).EncodeToPNG();
System.IO.File.WriteAllBytes("C:/Users/egsha/SavedScreen.png", bytes);
}
Texture2D toTexture2D(RenderTexture rTex)
{
Texture2D tex = new Texture2D(1920, 1080, TextureFormat.RGB24, false);
RenderTexture.active = rTex;
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
tex.Apply();
Destroy(tex);//prevents memory leak
return tex;
}
Your answer
Follow this Question
Related Questions
Save RawImage texture to file 0 Answers
Output texture has a material problem 0 Answers
SetTexture Not Working 1 Answer
Alternative for Graphics.CopyTexture() ? 2 Answers
Render to Texture artifacts 0 Answers