IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SaveRenderTexture.cs
1 using UnityEngine;
2 using System.Collections;
3 using System.IO;
4 
5 public class SaveRenderTexture : MonoBehaviour {
6 
7  // Use this for initialization
8  void Start () {
9  Debug.Log ("'SaveRenderTexture' active. Press T to save the UI render texture.");
10  }
11 
12  // Update is called once per frame
13  void Update () {
14  if (Input.GetKeyDown(KeyCode.T))
15  {
16  Save ();
17  }
18  }
19 
20  void Save() {
21  Camera cam = GameObject.Find ("UICamera").GetComponent<Camera> ();
22  RenderTexture renderTex = cam.targetTexture;
23  RenderTexture.active = renderTex;
24  Texture2D tex = new Texture2D (renderTex.width, renderTex.height, TextureFormat.ARGB32, false);
25  tex.ReadPixels (new Rect (0, 0, renderTex.width, renderTex.height), 0, 0);
26  tex.Apply ();
27  RenderTexture.active = null;
28 
29  byte[] data = tex.EncodeToPNG ();
30  File.WriteAllBytes("renderTexture.png", data);
31  Debug.Log ("Saved texture to: 'renderTexture.png'");
32  }
33 }