Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- namespace CodingJar
- {
- /**
- * This class adds a menu item which allows you to save out a render texture to a PNG for proper inspection.
- * It needs to go in an Editor/ folder.
- */
- public static class RenderTextureExporter
- {
- [MenuItem("Assets/Coding Jar/Save Render Texture", true)]
- static bool ValidateSaveRenderTexture()
- {
- RenderTexture rt = Selection.activeObject as RenderTexture;
- return ( rt != null );
- }
- [MenuItem("Assets/Coding Jar/Save Render Texture")]
- static void SaveRenderTexture()
- {
- RenderTexture rt = Selection.activeObject as RenderTexture;
- if ( rt == null )
- {
- EditorUtility.DisplayDialog( "Error", "You need to select a Render Texture", "OK" );
- return;
- }
- string fileName = EditorUtility.SaveFilePanel( "Save As", "Assets/", Selection.activeObject.name, "png" );
- if ( !string.IsNullOrEmpty(fileName) )
- {
- var oldRT = RenderTexture.active;
- RenderTexture.active = rt;
- Texture2D newTexture = new Texture2D( rt.width, rt.height, TextureFormat.ARGB32, false );
- // Write out the results...
- newTexture.ReadPixels( new Rect(0.0f, 0.0f, rt.width, rt.height), 0, 0, false );
- newTexture.Apply(false, false);
- System.IO.File.WriteAllBytes( fileName, newTexture.EncodeToPNG() );
- RenderTexture.active = oldRT;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment