2

我想从自定义 EditorWindow 中截取屏幕截图,我将其用作我正在开发的游戏的 LevelEditor,但我不知道该怎么做。

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

你能帮助我吗?谢谢!

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

4

1 回答 1

5

我为此使用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");
    }
}

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


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

屏幕截图以 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

在此处输入图像描述

于 2019-02-05T21:38:47.393 回答