编辑器窗口截图

Ara*_*raj 3 unity-game-engine unity3d-editor unity-editor

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

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

你能帮助我吗?谢谢!

编辑:我想在按下 GUILayout.Button 时通过代码截取屏幕截图:)

der*_*ugo 9

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

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

我宁愿添加一个带有 ShortCut 的全局,这样您实际上就可以截取任何当前活动的(上次单击/聚焦)MenuItem的屏幕截图!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");
    }
}
Run Code Online (Sandbox Code Playgroud)

因为它使用UnityEditorUnityEditorInternal确保将其放置在名为的文件夹中Editor,以将其从任何构建中排除。


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

屏幕截图以 PNG 文件形式放置,Assets/StreamingAssets名称中带有时间戳。

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


当前的快捷方式只是一个随机的快捷方式,到目前为止还没有使用过。[MenuItem(Screenshot/Take Screenshot %#k)]您可以按照MenuItem文档更改它

要创建热键,您可以使用以下特殊字符:

  • %ctrl在 Windows 上、cmd在 macOS 上)
  • # ( shift)
  • & ( alt)
  • 如果不需要特殊的修饰键组合,则可以在下划线之后给出该键。

例如,要使用热键 shift-alt-g 创建菜单,请使用“MyMenu/Do Something #&g”。g要创建带有热键且未按下任何键修饰符的菜单,请使用_(如“MyMenu/Do Something _g”)。

支持一些特殊的键盘按键作为热键,例如“#LEFT”将映射到左移。支持的按键有:LEFT、RIGHT、UP、DOWN、F1 .. F12、HOME、END、PGUP、PGDN。

热键文本前面必须有一个空格字符(“MyMenu/Do_g”不会被解释为热键,而“MyMenu/Do _g”会被解释为热键)。


实际操作中 - 我第一次在最后一次聚焦视图后只需按CTRL+ SHIFT+ ;第二次我聚焦并使用菜单项截屏KProjectInspector

在此输入图像描述