编辑器窗口截图

3
我想从我正在开发的游戏中用作LevelEditor的自定义EditorWindow中捕获屏幕截图,但我不确定如何做到这一点。
我想要的是捕获EditorWindow,而不是游戏视图或场景视图。
你能帮我吗?谢谢!
编辑:我想通过代码,在按下GUILayout.Button时拍摄屏幕截图 :)

1
展示一下你尝试过的内容。 - undefined
我认为他的意思是实现捕捉编辑器窗口并将其保存为图像文件的功能,就像Shaderforge使用的那样。请参考https://shaderforge.userecho.com/communities/1/topics/930-quickly-take-screenshot-of-entire-node-grid-at-appropriate-resolution。 - undefined
1个回答

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

由于它使用了UnityEditorUnityEditorInternal,请确保将其放置在名为Editor的文件夹中,以便在任何构建中排除它。
导入到您的项目后,只需使用CTRL + SHIFT + K 来创建当前活动的EditorWindow的截图。
屏幕截图以带有时间戳的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”将映射到shift-left。像这样支持的键是:LEFT、RIGHT、UP、DOWN、F1 .. F12、HOME、END、PGUP、PGDN。
热键文本必须以一个空格字符为前缀(“MyMenu/Do_g”不会被解释为热键,而“ MyMenu/Do _g”则会)。
如下图所示-第一次,在聚焦了Project视图后,我只需按下CTRL + SHIFT + K;第二次,我聚焦Inspector并使用菜单项来截取屏幕截图。 enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接