Created
March 6, 2023 12:18
-
-
Save unitycoder/644400471ecdec5e8a30f46ffaba36e9 to your computer and use it in GitHub Desktop.
SaveRTToFile Save Render Texture to File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://forum.unity.com/threads/speed-up-large-get-and-set-pixel-operations.1391098/ | |
| public class SaveRenderTextureToFile { | |
| [MenuItem("Assets/Save RenderTexture to file")] | |
| public static void SaveRTToFile() | |
| { | |
| RenderTexture rt = Selection.activeObject as RenderTexture; | |
| RenderTexture.active = rt; | |
| Texture2D tex = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false); | |
| 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