如何在没有像 Snipping Tool 或 Lightshot 这样的外部来源的情况下截取游戏视图的屏幕截图,例如使用我在游戏视图窗口中配置的分辨率截取屏幕截图。 就像我想为我的桌面、商店页面或与朋友分享 4k 屏幕截图一样。
Unity已经有截图工具了。它称为 Recorder,不需要任何编码。
这非常简单,最后你可以截取你在游戏视图中看到的所有内容的屏幕截图,如果你不想显示用户界面,只需禁用它的画布即可。
private void Update(){
if(Input.GetMouseButtonDown(0)){ // capture screen shot on left mouse button down
string folderPath = "Assets/Screenshots/"; // the path of your project folder
if (!System.IO.Directory.Exists(folderPath)) // if this path does not exist yet
System.IO.Directory.CreateDirectory(folderPath); // it will get created
var screenshotName =
"Screenshot_" +
System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss") + // puts the current time right into the screenshot name
".png"; // put youre favorite data format here
ScreenCapture.CaptureScreenshot(System.IO.Path.Combine(folderPath, screenshotName),2); // takes the sceenshot, the "2" is for the scaled resolution, you can put this to 600 but it will take really long to scale the image up
Debug.Log(folderPath + screenshotName); // You get instant feedback in the console
}
}
这里(页面底部)是一个脚本,它将在多个特定的屏幕尺寸下截取屏幕截图,还有一个有关使用它的视频。将其放在场景中的游戏对象上,确保脚本的类名和文件名相同