Search Unity

Exposing Unity Native ScreenShotting methods - useful screen shot tool for the editor.

Discussion in 'Editor & General Support' started by Noisecrime, Mar 19, 2021.

  1. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Whilst browsing through the Unity C# references I stumbled upon an internal class called 'ScreenShotting'. It contained a collection of useful methods to take screen shots from the editor. These looked useful so I wrote an editor script to expose them in the menu and via shortcut keys.

    You can find the script here InternalScreenShotting - gist and have posted the code below. Tested as of 2019.4.21f and the internal class still exists in 2020 so should still work.

    It should be noted that the 'Snap Active .. ' commands MUST be done via shortcut key as using the menu item will likely lead to incorrect screen shots.

    Methods
    • SetMainWindowSize (762,600) - sets the entire Unity window to 762 x 600
    • SetMainWindowSize (1024,768) - sets the entire Unity window to 1024 x 768
    • Snap Game View
    • Snap Active Window (use shortcut)
    • Snap Active Window Extended Right (use shortcut)
    • Snap Active Toolbar (use shortcut)
    • Snap Component ( upon rollover of component - sadly appears broken in 2019.4 )

    As far as I can tell these were most likely implemented long ago to provide a quick and easy way to grab screenshots of the editor for documentation. Unfortunately it appears Snap Component in 2019.4 is no longer working correctly, which is a real shame as this was very useful. Keep meaning to find the time to check out the internal Unity source code to see if its fixable.

    Screenshots
    GameView.png SceneView.png SceneViewExtended.png CameraInspector.png SceneViewToolbar.png


    Code

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. namespace NoiseCrimeStudios.Editor.Internal
    5. {
    6.     /// <summary>
    7.     /// Exposes the native Unity 'ScreenShotting' methods to Unity Menu & Shortcut keys.
    8.     /// </summary>
    9.     /// <remarks>
    10.     /// Once imported you can find the menu in either Window/Analysis/Screenshot or Window/Internal/Screenshot.
    11.     /// See UnityCsReference\Editor\Mono\GUI\ScreenShotting.cs
    12.     /// Hotkey: % (ctrl on Windows, cmd on macOS), # (shift), & (alt).
    13.     /// </remarks>
    14.     public static class InternalScreenShotting
    15.     {
    16.         static  System.Type screenShotsType = typeof(EditorApplication).Assembly.GetType("UnityEditor.ScreenShots", false);
    17.      
    18. #if UNITY_2018_2_OR_NEWER
    19.         const string m_MenuItemName = "Window/Analysis/Screenshot/";
    20. #else
    21.         const string m_MenuItemName = "Window/Internal/Screenshot/";
    22. #endif      
    23.      
    24.         /// <summary>Sets the entire Unity Window to 762x600.</summary>
    25.         [MenuItem( m_MenuItemName + "SetMainWindowSize ( 762,600)", false, 115)]
    26.         public static void SetMainWindowSizeSmall()
    27.         {
    28.             var method          = screenShotsType.GetMethod("SetMainWindowSizeSmall");
    29.             if ( null != method ) method.Invoke(null, null);
    30.         }
    31.  
    32.         /// <summary>Sets the entire Unity Window to 1024x768.</summary>
    33.         [MenuItem( m_MenuItemName + "SetMainWindowSize (1024,768)", false, 115)]
    34.         public static void SetMainWindowSize()
    35.         {          
    36.             var method          = screenShotsType.GetMethod("SetMainWindowSize");
    37.             if ( null != method ) method.Invoke(null, null);
    38.         }
    39.      
    40.         /// <summary>Screenshots the Game View Window.</summary>  
    41.         [MenuItem( m_MenuItemName + "Snap Game View Content %&g", false, 115)]
    42.         public static void ScreenGameViewContent()
    43.         {
    44.             Debug.Log("Snap Game View Content");
    45.             var method          = screenShotsType.GetMethod("ScreenGameViewContent");
    46.             if ( null != method ) method.Invoke(null, null);
    47.         }
    48.  
    49.         /// <summary>Screenshots the active Window.</summary>
    50.         [MenuItem( m_MenuItemName + "Snap Active Window %&h", false, 115)]
    51.         public static void Screenshot()
    52.         {
    53.             Debug.Log("Snap Active Window");
    54.             var method          = screenShotsType.GetMethod("Screenshot");
    55.             if ( null != method ) method.Invoke(null, null);
    56.         }
    57.      
    58.         /// <summary>Screenshots the active Window and the rest of the screen to the right. For example Sceneview & Inspector.</summary>
    59.         [MenuItem( m_MenuItemName + "Snap Active Window Extended Right %&j", false, 115)]
    60.         public static void ScreenshotExtendedRight()
    61.         {
    62.             Debug.Log("Snap Active Window Extended Right");
    63.             var method          = screenShotsType.GetMethod("ScreenshotExtendedRight");
    64.             if ( null != method ) method.Invoke(null, null);
    65.         }
    66.  
    67.         /// <summary>Screenshots the active window toolbar. Bit unreliable.</summary>
    68.         [MenuItem( m_MenuItemName + "Snap Active Toolbar %&k", false, 115)]
    69.         public static void ScreenshotToolbar()
    70.         {
    71.             Debug.Log("Screenshot Active Toolbar");
    72.             var method          = screenShotsType.GetMethod("ScreenshotToolbar");
    73.             if ( null != method ) method.Invoke(null, null);
    74.         }
    75.  
    76.         /// <summary>Screenshots the Component you rollover next. Bit unreliable.</summary>
    77.         [MenuItem( m_MenuItemName + "Snap Component on Rollover %&l", false, 115)]
    78.         public static void ScreenShotComponent()
    79.         {
    80.             Debug.Log("Snap Component - Waiting for rollover on taget component");
    81.             var method          = screenShotsType.GetMethod("ScreenShotComponent", new System.Type[0]);
    82.             if ( null != method ) method.Invoke(null, new object[0]);
    83.         }
    84.     }
    85. }
    86.  
    87. /*
    88. * var assembly        = typeof(EditorApplication).Assembly;
    89. * var screenShotsType = assembly.GetType("UnityEditor.ScreenShots", true);
    90. *
    91. * static Type unityEditorScreenShotsClassType;
    92. * static Type GetScreenShotsClassType() {    return typeof(EditorApplication).Assembly.GetType("UnityEditor.ScreenShots", true); }
    93. */
     
    mgear likes this.