AlicizaX/Client/Assets/Editor/Tools/ScreenShotWindow.cs
2025-01-26 20:55:39 +08:00

72 lines
2.5 KiB
C#

using UnityEngine;
using UnityEditor;
using System;
using AlicizaX.EditorExtension.Editor;
namespace BuildCli
{
public class CaptureWindow : EditorWindow
{
private string saveFileName = string.Empty;
private string saveDirPathKey = "CaptureSaveDirPathKey";
[UnityEditor.MenuItem("开发工具/常用Tools/截图工具", false, 102)]
private static void Capture()
{
if (HasOpenInstances<CaptureWindow>())
{
EditorWindow.GetWindow<CaptureWindow>("截图工具").Close();
}
else
{
EditorWindow.GetWindow<CaptureWindow>("截图工具");
}
}
private void OnGUI()
{
string buildPath = GameEditorPrefs.GetString(saveDirPathKey, Application.dataPath);
EditorGUILayout.LabelField("输出目录 : ");
EditorGUILayout.LabelField(buildPath);
if (GUILayout.Button("选择目录"))
{
string path = EditorUtility.OpenFolderPanel("选择目录", buildPath, Application.dataPath);
if (!string.IsNullOrEmpty(path))
{
GameEditorPrefs.SetString(saveDirPathKey, path);
}
}
if (GUILayout.Button("打开目录"))
{
string openPath = GameEditorPrefs.GetString(saveDirPathKey, Application.dataPath);
System.Diagnostics.Process.Start(openPath);
}
// insert blank line
GUILayout.Label("");
if (GUILayout.Button("截图"))
{
var resolution = GetMainGameViewSize();
int x = (int)resolution.x;
int y = (int)resolution.y;
Debug.Log(buildPath);
var outputPath = buildPath + "/" + DateTime.Now.ToString($"{x}x{y}_yyyy_MM_dd_HH_mm_ss") + ".png";
ScreenCapture.CaptureScreenshot(outputPath);
Debug.Log("保存路径:" + outputPath);
}
}
public static Vector2 GetMainGameViewSize()
{
System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
System.Reflection.MethodInfo GetSizeOfMainGameView = T.GetMethod("GetSizeOfMainGameView",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
System.Object Res = GetSizeOfMainGameView.Invoke(null, null);
return (Vector2)Res;
}
}
}