【问题标题】:Editor Window screenshot编辑器窗口截图
【发布时间】:2018-02-07 11:49:29
【问题描述】:

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

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

你能帮帮我吗? 谢谢!

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

【问题讨论】:

标签: unity3d unity3d-editor unity-editor


【解决方案1】:

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

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

我宁愿用快捷方式添加一个全局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");
    }
}

由于它使用 UnityEditor 和 UnityEditorInternal,请确保将其放在名为 Editor 的文件夹中,以将其从任何构建中排除。


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

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

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


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

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

  • %(Windows 上为 ctrl,macOS 上为 cmd)
  • # (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 + K 在最后聚焦 Project 视图后;第二次我聚焦Inspector并使用菜单项截取屏幕截图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    相关资源
    最近更新 更多