DDDD
This commit is contained in:
53
Assets/Scripts/Utils/ScreenshotCapturer.cs
Normal file
53
Assets/Scripts/Utils/ScreenshotCapturer.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
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
|
||||
}
|
||||
2
Assets/Scripts/Utils/ScreenshotCapturer.cs.meta
Normal file
2
Assets/Scripts/Utils/ScreenshotCapturer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c9b2dc114877d3479675dbe81e42739
|
||||
Reference in New Issue
Block a user