54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using System.IO;
|
|
|
|
public class ScreenshotCapturer : MonoBehaviour
|
|
{
|
|
#if UNITY_EDITOR
|
|
private void Update()
|
|
{
|
|
|
|
if (Input.GetKeyDown(KeyCode.F12))
|
|
{
|
|
CaptureScreenshot();
|
|
}
|
|
|
|
}
|
|
|
|
private void CaptureScreenshot()
|
|
{
|
|
// 获取Assets上一级目录路径
|
|
string projectPath = Directory.GetParent(Application.dataPath).FullName;
|
|
string screenshotDir = Path.Combine(projectPath, "Screenshots");
|
|
|
|
// 如果目录不存在则创建
|
|
if (!Directory.Exists(screenshotDir))
|
|
{
|
|
Directory.CreateDirectory(screenshotDir);
|
|
}
|
|
|
|
// 生成基于时间的文件名
|
|
string timestamp = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
|
|
string filename = $"Screenshot_{timestamp}.png";
|
|
string fullPath = Path.Combine(screenshotDir, filename);
|
|
|
|
// 确保文件名唯一
|
|
int counter = 1;
|
|
while (File.Exists(fullPath))
|
|
{
|
|
filename = $"Screenshot_{timestamp}_{counter}.png";
|
|
fullPath = Path.Combine(screenshotDir, filename);
|
|
counter++;
|
|
}
|
|
|
|
// 截取屏幕
|
|
ScreenCapture.CaptureScreenshot(fullPath);
|
|
Debug.Log($"Screenshot saved to: {fullPath}");
|
|
|
|
// 刷新资源数据库(如果需要)
|
|
AssetDatabase.Refresh();
|
|
}
|
|
#endif
|
|
}
|