Skip to content

Instantly share code, notes, and snippets.

@paulhayes
Created March 26, 2021 09:53
Show Gist options
  • Save paulhayes/0d682620fa6bb1169ae28abf7cde8889 to your computer and use it in GitHub Desktop.
Save paulhayes/0d682620fa6bb1169ae28abf7cde8889 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
public class SaveRenderTextureToFile {
[MenuItem("Assets/Save RenderTexture to file")]
public static void SaveRTToFile()
{
RenderTexture rt = Selection.activeObject as RenderTexture;
Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
RenderTexture tmp = RenderTexture.GetTemporary(tex.width,tex.height,0,RenderTextureFormat.ARGB32);
Graphics.Blit(rt,tmp);
RenderTexture.active = tmp;
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
RenderTexture.active = null;
byte[] bytes;
bytes = tex.EncodeToPNG();
string path = AssetDatabase.GetAssetPath(rt) + ".png";
System.IO.File.WriteAllBytes(path, bytes);
AssetDatabase.ImportAsset(path);
Debug.Log("Saved to " + path);
}
[MenuItem("Assets/Save RenderTexture to file", true)]
public static bool SaveRTToFileValidation()
{
return Selection.activeObject is RenderTexture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment