我想从我正在开发的游戏中用作LevelEditor的自定义EditorWindow中捕获屏幕截图,但我不确定如何做到这一点。
我想要的是捕获EditorWindow,而不是游戏视图或场景视图。
你能帮我吗?谢谢!
编辑:我想通过代码,在按下GUILayout.Button时拍摄屏幕截图 :)
我想要的是捕获EditorWindow,而不是游戏视图或场景视图。
你能帮我吗?谢谢!
编辑:我想通过代码,在按下GUILayout.Button时拍摄屏幕截图 :)
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
的文件夹中,以便在任何构建中排除它。Assets/StreamingAssets
中。[MenuItem(Screenshot/Take Screenshot %#k)]
后更改它。Project
视图后,我只需按下CTRL + SHIFT + K;第二次,我聚焦Inspector
并使用菜单项来截取屏幕截图。