Untiy实现截图Game视图工具 方便快捷 不再依赖微信/QQ或者其他截图工具进行截图 并且可以自定义路径和分辨率!!!
下面是代码:
using System.IO;
using UnityEditor;
using UnityEngine;
public class ScreenCaptureEditor : EditorWindow
{
private static string directory = "Screenshots/Capture/";
private static string latestScreenshotPath = "";
private bool initDone = false;
private GUIStyle BigText;
void InitStyles()
{
initDone = true;
BigText = new GUIStyle(GUI.skin.label)
{
fontSize = 20,
fontStyle = FontStyle.Bold,
alignment = TextAnchor.MiddleCenter,
normal = new GUIStyleState() { textColor = Color.green }
};
}
private void OnGUI()
{
if (!initDone)
{
InitStyles();
}
GUILayout.Label("游戏窗口截图", BigText);
if (GUILayout.Button("截图"))
{
TakeScreenshot(1);
}
GUILayout.Label("分辨率: " + GetResolution(1));
if (GUILayout.Button("双倍分辨率截图"))
{
TakeScreenshot(2);
}
GUILayout.Label("分辨率: " + GetResolution(2));
if (GUILayout.Button("打开目录"))
{
ShowFolder();
}
GUILayout.Label("保存目录: " + directory);
}
[MenuItem("Tools/截图")]
public static void ShowWindow()
{
var window = GetWindowWithRect<ScreenCaptureEditor>(new Rect(0, 0, 180, 250));
window.titleContent = new GUIContent("截图工具");
window.Focus();
window.Repaint();
}
private static void ShowFolder()
{
if (File.Exists(latestScreenshotPath))
{
EditorUtility.RevealInFinder(latestScreenshotPath);
return;
}
Directory.CreateDirectory(directory);
EditorUtility.RevealInFinder(directory);
}
private static void TakeScreenshot(int super)
{
Directory.CreateDirectory(directory);
var currentTime = System.DateTime.Now;
var filename = currentTime.ToString().Replace('/', '-').Replace(':', '_') + ".png";
var path = directory + filename;
ScreenCapture.CaptureScreenshot(path, super);
latestScreenshotPath = path;
Debug.Log($"Screenshot saved: <b>{path}</b> with resolution <b>{GetResolution(super)}</b>");
}
private static string GetResolution(int super)
{
Vector2 size = UnityEditor.Handles.GetMainGameViewSize();
Vector2Int sizeInt = new Vector2Int((int)size.x, (int)size.y);
return $"{sizeInt.x * super}x{sizeInt.y * super}";
}
}