Search Unity

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,042
    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:
    249
    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:
    30
    We've got the same problem here.
     
  5. Izitmee

    Izitmee

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