Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

How to save screenshot of Editor Window?

Discussion in 'Scripting' started by shinriyo_twitter, Sep 7, 2012.

  1. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
    Hi there.

    I want to save screenshot of Editor Window.
    Unity have "Application.CaptureScreenshot" method, but it captures only Scene view.
    I use plugin which show the picture on Editor's window like SmoothMoves.
    If I take picture on the scene view, the camera make contrasting(?). so, in spite of I set the orthgraphic(non-persepective)..

    Do you know how to capture screenshot of Editor Window?
    However, SmoothMoves source was compiled so I can't extend the source...
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    You could be in luck with setting the active RenderTexture at the beginning of your EditorWindow OnGUI, if Event.current.type is EventType.Repaint and then setting it back afterwards.

    Note that if it works, rendering would go to the texture in stead of the window, so it would blink.

    I've not tried this with EditorWindows before, but I do know that it works in the runtime.

    Note that repaints do not automatically get fired all of the time in the editor, so you might have to also do an editorWidow.Repaint call when you do your screenshot.
     
  3. CanisLupus

    CanisLupus

    Joined:
    Jul 29, 2013
    Posts:
    427
    For future users stumbling here, user Ryiah has working code for this (no RenderTexture needed):
    https://forum.unity.com/threads/bui...eenshot-of-the-inspector.564673/#post-3754564

    This was for the Inspector, so I modified it to work for a custom EditorWindow script. You should place this method inside one and call it whenever you need it:
    Code (CSharp):
    1. private void CaptureWindowScreenshot()
    2. {
    3.     int width = (int) this.position.width;
    4.     int height = (int) this.position.height;
    5.  
    6.     var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
    7.     tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    8.     tex.Apply();
    9.  
    10.     byte[] bytes = tex.EncodeToPNG();
    11.     System.IO.Directory.CreateDirectory("Screenshots");
    12.     System.IO.File.WriteAllBytes("Screenshots/Screenshot " + System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss.fff") + ".png", bytes);
    13. }
    It creates a new texture with the dimensions of the editor window and reads the pixels into it. Then encodes the result to a png file in a folder "Screenshots" at the root of your project (outside "Assets").



    Suggestion: add this as a context menu option (in the dropdown menu at the top right of your editor window.
    Code (CSharp):
    1. public class CustomWindow : EditorWindow, IHasCustomMenu
    2. {
    3.     // ...
    4.  
    5.     void IHasCustomMenu.AddItemsToMenu(GenericMenu menu)
    6.     {
    7.         menu.AddItem(new GUIContent("Screenshot"), false, () => CaptureWindowScreenshot());
    8.     }
    9.  
    10.     private void CaptureWindowScreenshot()
    11.     {
    12.         int width = (int) this.position.width;
    13.         int height = (int) this.position.height;
    14.  
    15.         var tex = new Texture2D(width, height, TextureFormat.RGB24, false);
    16.         tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
    17.         tex.Apply();
    18.  
    19.         byte[] bytes = tex.EncodeToPNG();
    20.         System.IO.Directory.CreateDirectory("Screenshots");
    21.         System.IO.File.WriteAllBytes("Screenshots/Screenshot " + System.DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss.fff") + ".png", bytes);
    22.     }
    23. }
    WARNING: it may fail with an error about reading out of bounds, especially if you just resized the window. If you repeat it, it should succeed. Currently don't know why this happens.
     
  4. kevin-masson

    kevin-masson

    Joined:
    Sep 24, 2018
    Posts:
    67
    We've got the same problem here.
     
  5. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Anybody found a solution to the above out of bounds problem? :B
     
  6. sbsmith

    sbsmith

    Joined:
    Feb 7, 2013
    Posts:
    112
    Possible solution for Unity 2018.3.

    I was poking at this today and was having trouble because the tool triggering the capture was on a different window on a different screen and I couldn't consistently get access to the inspector's RenderTexture. This uses UnityEditorInternal.InternalEditorUtility.ReadScreenPixel instead of Texture2D.ReadPixels

    Code (CSharp):
    1. private static void CaptureWindowScreenshot()
    2. {
    3.     EditorWindow inspectorWindow = EditorWindow.GetWindow( typeof( Editor ).Assembly.GetType( "UnityEditor.InspectorWindow" ) );
    4.     inspectorWindow.Focus();
    5.  
    6.     EditorApplication.delayCall += () =>
    7.     {
    8.         int inspectorWidth = (int)inspectorWindow.position.width;
    9.         int inspectorHeight = (int)inspectorWindow.position.height;
    10.  
    11.         Color[] pixels = UnityEditorInternal.InternalEditorUtility.ReadScreenPixel( inspectorWindow.position.position, inspectorWidth, inspectorHeight );
    12.  
    13.         Texture2D inspectorTexture = new Texture2D( inspectorWidth, inspectorHeight, TextureFormat.RGB24, false );
    14.         inspectorTexture.SetPixels( pixels );
    15.  
    16.         byte[] bytes = inspectorTexture.EncodeToPNG();
    17.         System.IO.File.WriteAllBytes( Application.dataPath + "/InspectorScreenshot.png", bytes );
    18.     };
    19. }
     
    kevin-masson likes this.
  7. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. int inspectorWidth = (int)inspectorWindow.position.width * 2;
    2. int inspectorHeight = (int)inspectorWindow.position.height * 2;
     
    Last edited by a moderator: Mar 29, 2020