c# - 如何使用 C# 在 Unity 中截取我的桌面应用程序的当前 View 的屏幕截图?

标签 c# unity3d desktop-application

我有一个在 Unity 上制作的桌面应用程序,我想使用附加到主摄像头的 C# 脚本截取应用程序中当前 View 的屏幕截图。请帮忙。

我浏览了在此平台上找到的其他代码片段,但似乎没有任何帮助。

最佳答案

你可以使用 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. 我使用逐字字符串来定义您的文件夹
  2. 你存放截图的文件夹必须存在(如果你想在脚本中创建它 you can follow this answer )
  3. AFTER COMMENT REQUEST - 1:如果您没有设置文件夹,文件将保存在应用程序的默认目录中(根据系统而变化 - 您可以从 Application.dataPath 开始检查)
  4. AFTER COMMENT REQUEST - 2:如果您使用相同的路径和文件名,文件将被覆盖,因此我添加了一种方法让您保存多个屏幕截图,同样在不同的 session 中使用 PlayerPrefs

关于c# - 如何使用 C# 在 Unity 中截取我的桌面应用程序的当前 View 的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56899767/

相关文章:

c# - 放大/缩小时Unity相机平移

c# - INSERT 语句的外键冲突

swing - 如何在NetBeans中删除actionPerformed方法

c# - 如何拥有 DataTemplates 类?

c# - 如何更改 Unity 3D 中的默认粒子?

c# - 测试期间的 EF Core 内部缓存和许多 DbContext 类型

c# - 检查玩家在潜行游戏中的亮度

java - NullPointerException 与 libgdx 使用 gdx render() 方法

c# - 在 C# 中调用泛型函数

c# - 如何防止 Windows (Phone) 8.1 通用应用程序中的锁屏?