Unity 截图错误:也捕获了编辑器

2024-01-08

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

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 在内的、仅限游戏视图的截图?

Note,UI的事情,我在网上找到了其他方法来截图(使用RenderTextures)但这些不包括用户界面。在我的另一个“真实”项目中,我也有 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Unity 截图错误:也捕获了编辑器 的相关文章

随机推荐