作为脚本附到GameObject上,我选择附到相机上
Screenshot.csview raw 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38// 创建这个脚本,挂在任意 GameObject 上
using UnityEngine;
using System.IO;
public class Screenshot : MonoBehaviour
{
[]
public KeyCode screenshotKey = KeyCode.F12;
public int superSize = 1; // 1 = 原始分辨率, 2 = 2倍分辨率
void Update()
{
if (Input.GetKeyDown(screenshotKey))
{
TakeScreenshot();
}
}
void TakeScreenshot()
{
string folderPath = Path.Combine(Application.dataPath, "..", "Screenshots");
// 创建文件夹
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
// 生成文件名
string fileName = $"Screenshot_{System.DateTime.Now:yyyy-MM-dd_HH-mm-ss}.png";
string filePath = Path.Combine(folderPath, fileName);
// 截图
ScreenCapture.CaptureScreenshot(filePath, superSize);
Debug.Log($"截图已保存: {filePath}");
}
}修改编辑器UI,作为菜单使用;需要放到scene文件同目录的Editor目录下
效果如下:Editor/ScreenShotTest.csview raw 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34using System.IO;
using System.Threading.Tasks;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
public class ScreenShotTest : MonoBehaviour
{
//ctrl + shift + y 截图
[]
private static async void Screenshot()
{
string folderPath = Directory.GetCurrentDirectory() + "\\Screenshots";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
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);
ScreenCapture.CaptureScreenshot(Path.Combine(folderPath , stampString + ".png"));
Debug.Log("截图中......");
//等待5秒
await Task.Delay(5000);
System.Diagnostics.Process.Start("explorer.exe", folderPath);
Debug.Log("截图" + stampString + ".png");
}
void OnMouseDown()
{
ScreenCapture.CaptureScreenshot("SomeLevel.png");
}
}