编辑 Window 截图

Editor Window screenshot

我想从自定义 EditorWindow 中捕获屏幕截图,我将其用作我正在开发的游戏的关卡编辑器,但我不确定如何操作。

我想要的是捕获 EditorWindow,而不是游戏视图或场景视图。

你能帮帮我吗? 谢谢!

编辑:我想在按 GUILayout.Button :)

时按代码截屏

我使用 InternalEditorUtility.ReadScreenPixel 为此编写了一个脚本。

起初我确实按照您在 GUILayout.Button 上的要求得到了它,但决定在这里提供一个更通用的解决方案,它完全独立于 EditorWindow 本身。
(无论如何,您仍然可以从 GUILayout.Button 调用 EditorScreenshotExtension.Screenshot();。)

我宁愿用 ShortCut 添加一个全局 MenuItem 这样你实际上可以截取 任何 当前活动的(最后一个 clicked/focused) EditorWindow!

EditorScreenshotExtension.cs

using System.IO;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

public static class EditorScreenshotExtension
{
    [MenuItem("Screenshot/Take Screenshot %#k")]
    private static void Screenshot()
    {
        // Get actvive EditorWindow
        var activeWindow = EditorWindow.focusedWindow;

        // Get screen position and sizes
        var vec2Position = activeWindow.position.position;
        var sizeX = activeWindow.position.width;
        var sizeY = activeWindow.position.height;

        // Take Screenshot at given position sizes
        var colors = InternalEditorUtility.ReadScreenPixel(vec2Position, (int)sizeX, (int)sizeY);

        // write result Color[] data into a temporal Texture2D
        var result = new Texture2D((int)sizeX, (int)sizeY);
        result.SetPixels(colors);

        // encode the Texture2D to a PNG
        // you might want to change this to JPG for way less file size but slightly worse quality
        // if you do don't forget to also change the file extension below
        var bytes = result.EncodeToPNG();

        // In order to avoid bloading Texture2D into memory destroy it
        Object.DestroyImmediate(result);

        // finally write the file e.g. to the StreamingAssets folder
        var timestamp = System.DateTime.Now;
        var stampString = string.Format("_{0}-{1:00}-{2:00}_{3:00}-{4:00}-{5:00}", timestamp.Year, timestamp.Month, timestamp.Day, timestamp.Hour, timestamp.Minute, timestamp.Second);
        File.WriteAllBytes(Path.Combine(Application.streamingAssetsPath, "Screenshot" + stampString + ".png"), bytes);

        // Refresh the AssetsDatabase so the file actually appears in Unity
        AssetDatabase.Refresh();

        Debug.Log("New Screenshot taken");
    }
}

因为它使用 UnityEditorUnityEditorInternal 确保将它放在一个名为 Editor 的文件夹中以将其排除在任何构建之外。


将其导入到您的项目后,只需使用 CTRL + SHIFT + K 即可创建当前活动的 EditorWindow 的屏幕截图。

屏幕截图作为 PNG 文件放置在 Assets/StreamingAssets 中,名称中带有时间戳。

它还会在 UniytEditor 的顶部菜单中添加一个条目。


当前的快捷方式只是一个随机的,目前还没有使用过。您可以在 [MenuItem(Screenshot/Take Screenshot %#k)] 中根据 MenuItem 文档

更改它

To create a hotkey you can use the following special characters:

  • % (ctrl on Windows, cmd on macOS)
  • # (shift)
  • & (alt)
  • If no special modifier key combinations are required the key can be given after an underscore.

For example to create a menu with hotkey shift-alt-g use "MyMenu/Do Something #&g". To create a menu with hotkey g and no key modifiers pressed use _ like "MyMenu/Do Something _g".

Some special keyboard keys are supported as hotkeys, for example "#LEFT" would map to shift-left. The keys supported like this are: LEFT, RIGHT, UP, DOWN, F1 .. F12, HOME, END, PGUP, PGDN.

A hotkey text must be preceded with a space character ("MyMenu/Do_g" won't be interpreted as hotkey, while "MyMenu/Do _g" will).


实际操作 - 第一次我只是按 CTRL + SHIFT + K最后聚焦 Project 视图之后;第二次我聚焦 Inspector 并使用菜单项截取屏幕截图