gpt4 book ai didi

c# - Unity截图报错: capturing the editor too

转载 作者:行者123 更新时间:2023-11-30 23:09:18 28 4
gpt4 key购买 nike

我正在尝试创建一些屏幕截图,但 ScreenCapture.CaptureScreenshot 实际上捕获了整个编辑器,而不仅仅是游戏 View 。

error

public class ScreenShotTaker : MonoBehaviour
{
public KeyCode takeScreenshotKey = KeyCode.S;
public int screenshotCount = 0;
private void Update()
{
if (Input.GetKeyDown(takeScreenshotKey))
{
ScreenCapture.CaptureScreenshot("Screenshots/"
+ "_" + screenshotCount + "_"+ Screen.width + "X" + Screen.height + "" + ".png");
Debug.Log("Screenshot taken.");
}
}
}

可能是什么问题?如何截取包含 UI 的体面的、仅游戏 View 的屏幕截图?

注意,关于 UI,我在网上找到了其他截屏方法(使用 RenderTextures),但这些方法不包括 UI。在我的另一个“真实”项目中,我也有 UI,我刚刚打开了这个测试项目,看看屏幕截图问题是否也存在于此。

最佳答案

这是一个错误,我建议您暂时远离它,直到 ScreenCapture.CaptureScreenshot 足够成熟。此功能是在 Unity 2017.2 beta 中添加的,因此现在是向编辑器提交错误报告的最佳时机。更糟糕的是,它在我的计算机上只保存黑色和空白图像。


至于截屏,还有其他方法可以在没有 RenderTextures 的情况下执行此操作,这也将在屏幕截图中包含 UI。

您可以使用 Texture2D.ReadPixels 从屏幕读取像素,然后使用 File.WriteAllBytes 保存它。

public KeyCode takeScreenshotKey = KeyCode.S;
public int screenshotCount = 0;

private void Update()
{
if (Input.GetKeyDown(takeScreenshotKey))
{
StartCoroutine(captureScreenshot());
}
}

IEnumerator captureScreenshot()
{
yield return new WaitForEndOfFrame();
string path = "Screenshots/"
+ "_" + screenshotCount + "_" + Screen.width + "X" + Screen.height + "" + ".png";

Texture2D screenImage = new Texture2D(Screen.width, Screen.height);
//Get Image from screen
screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenImage.Apply();
//Convert to png
byte[] imageBytes = screenImage.EncodeToPNG();

//Save image to file
System.IO.File.WriteAllBytes(path, imageBytes);
}

关于c# - Unity截图报错: capturing the editor too,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45886181/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com