如何使用C#拍摄Unity中桌面应用程序当前视图的屏幕截图? [英] How do I take a screenshot of the current view of my desktop app in Unity using C#?
本文介绍了如何使用C#拍摄Unity中桌面应用程序当前视图的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个在Unity上制作的桌面应用程序,我想使用主摄像头附带的C#脚本在该应用程序中获取当前视图的屏幕快照.请帮忙.
I have a desktop application that I have made on Unity and I want to take a screenshot of my current view in the application using a C# script attached to the main camera. Please help.
我浏览了在此平台上找到的其他代码段,但似乎无济于事.
I have browsed other code snippets that I found on this platform and nothing seemed to help.
推荐答案
您可以使用CaptureScreenshot .
public class ScreenCapture : MonoBehaviour
{
//here you can set the folder you want to use,
//IMPORTANT - use "@" before the string, because this is a verbatim string
//IMPORTANT - the folder must exists
string pathToYourFile = @"C:\Screenshots\";
//this is the name of the file
string fileName = "filename";
//this is the file type
string fileType = ".png";
private int CurrentScreenshot { get => PlayerPrefs.GetInt("ScreenShot"); set => PlayerPrefs.SetInt("ScreenShot", value); }
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
UnityEngine.ScreenCapture.CaptureScreenshot(pathToYourFile + fileName + CurrentScreenshot + fileType);
CurrentScreenshot++;
}
}
}
一些注意事项.
- 我使用了一个逐字字符串来定义您的文件夹
- 存储屏幕快照的文件夹必须存在(如果要在脚本 Application.dataPath )
- 之后的注释请求-2 :如果您使用相同的路径和文件名,则文件将被覆盖,因此我添加了一种方法,可以让您保存多个屏幕截图,也可以在不同的会话中使用 PlayerPrefs .
- I used a verbatim string to define your folder
- The folder where you store the screenshot must exist (if you want to create it in the script you can follow this answer)
- AFTER COMMENT REQUEST - 1: If you do not set the folder the file will be saved in the default directory of the application (that changes based on the system - you can check it from Application.dataPath)
- AFTER COMMENT REQUEST - 2: If you use the same path and filename the file will be overriden, so I added a way to let you save multiple screenshots, also in different sessions using PlayerPrefs.
这篇关于如何使用C#拍摄Unity中桌面应用程序当前视图的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文