Skip to content

Instantly share code, notes, and snippets.

@desplesda
Created January 29, 2020 01:31
Show Gist options
  • Save desplesda/39c3d6f5b3683dfe4e1397ca32338d59 to your computer and use it in GitHub Desktop.
Save desplesda/39c3d6f5b3683dfe4e1397ca32338d59 to your computer and use it in GitHub Desktop.
Methods for capturing screenshots of the game view and the inspector in Unity (2018.3+)
// Drop these methods into a class in a folder named 'Editor'
[MenuItem("X/Capture Game &1")]
static void CaptureGameScreenshot()
{
EditorApplication.delayCall += () =>
{
var sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
int i = 0;
string path;
do
{
i += 1;
path = $"Screenshots/{sceneName}-{i}.png";
} while (System.IO.File.Exists(path));
// We'd use CaptureScreenshotAsTexture here but it's bugged in
// 2018.3 - it renders the Scene view :(
ScreenCapture.CaptureScreenshot(path);
};
}
// Screenshot capturing
[MenuItem("X/Capture Inspector &3")]
static void CaptureInspectorScreenshot()
{
// https://forum.unity.com/threads/built-in-method-to-take-a-screenshot-of-the-inspector.564673/#post-4715168
EditorWindow inspectorWindow = EditorWindow
.GetWindow(typeof(Editor).Assembly.GetType("UnityEditor.InspectorWindow"));
inspectorWindow.Focus();
EditorApplication.delayCall += () =>
{
int inspectorWidth = (int)inspectorWindow.position.width * 2;
int inspectorHeight = (int)inspectorWindow.position.height * 2;
Color[] pixels = UnityEditorInternal.InternalEditorUtility.ReadScreenPixel(inspectorWindow.position.position, inspectorWidth, inspectorHeight);
Texture2D inspectorTexture = new Texture2D(inspectorWidth, inspectorHeight, TextureFormat.RGB24, false);
inspectorTexture.SetPixels(pixels);
byte[] bytes = inspectorTexture.EncodeToPNG();
var sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
int i = 0;
string path;
do
{
i += 1;
path = $"Screenshots/{sceneName}-Inspector-{i}.png";
} while (System.IO.File.Exists(path));
System.IO.File.WriteAllBytes(path, bytes);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment