- Home /
How to save a RenderTexture to a png from a different camera?
I am creating a camera that allows the player to take pictures in game. I am using a separate camera other than the MainCamera that renders to a RenderTexture for the "fake" camera screen. Right now it's taking a picture of what the MainCamera sees, which I understand. What I'm trying to figure out is how to take/save a png from that RenderTexture when the player wants to take a picture. The code that I'm using is essentially from the Texture2D Script Reference. I'm not sure if Screen.width/height will work in this situation either unless I can find a way to access the "fake" camera's projection width/height.
void TakePic()
{
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply ();
var bytes = tex.EncodeToPNG();
File.WriteAllBytes("Sample.png", bytes);
Destroy(tex);
}
Answer by whydoidoit · Apr 29, 2013 at 08:26 PM
You just need the width and height of the render texture you made, that's the size to copy to the Texture2D.