c# - 如何使用 C# 在 Unity 中截取我的桌面应用程序的当前视图?
问题描述
我有一个在 Unity 上制作的桌面应用程序,我想使用附加到主摄像头的 C# 脚本截取应用程序中当前视图的屏幕截图。请帮忙。
我浏览了在这个平台上找到的其他代码片段,但似乎没有任何帮助。
解决方案
您可以使用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++;
}
}
}
一些笔记。
- 我使用逐字字符串来定义您的文件夹
- 您存储屏幕截图的文件夹必须存在(如果您想在脚本中创建它,您可以按照这个答案)
- 后评论请求 - 1:如果您没有设置文件夹,文件将保存在应用程序的默认目录中(根据系统更改 - 您可以从Application.dataPath检查它)
- 后评论请求 - 2:如果您使用相同的路径和文件名,该文件将被覆盖,所以我添加了一种方法让您保存多个屏幕截图,也可以在不同的会话中使用PlayerPrefs。
推荐阅读
- sql - 我必须创建一个具有以下属性的视图
- python - python中的无头chrome - 配置选项
- java - 如何去活动形成一个recycleview片段
- assembly - NASM:%include 文件中操作码和操作数的组合无效,报告的行号超出了末尾?
- django - 为什么 Django 在迁移时会更新其他应用程序/数据库?
- github - 如何解决我要上传的工件?
- android - Kotlin 适配器删除项目
- gnuradio - GNURADIO 中的 catch_exceptions
- woocommerce - 使用 WooCommerce API 按价格排序
- pytorch - 如何找回只有权重字典的 pytorch 模型的架构?