【问题标题】:Unity screenshot error: capturing the editor toounity截图错误:也捕获编辑器
【发布时间】:2017-08-26 01:23:25
【问题描述】:

我正在尝试创建一些屏幕截图,但 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 的游戏视图屏幕截图?

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

【问题讨论】:

  • 用户界面有什么用?
  • 其他一些使用RenderTextures 的方法不起作用,因为它们不包含用户界面。

标签: c# unity3d screenshot


【解决方案1】:

这是一个错误,我建议您暂时远离它,直到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);
}

【讨论】:

  • 谢谢,它主要工作。唯一的问题是我的 UI 元素使用 Outline 并且轮廓值始终是它们的默认白色。我在初始化期间更改了它们,但屏幕截图忽略了它。现在,我应该手动更改它们吗? (自动着色的原因是他们从关卡材料中获取颜色来匹配)。
  • 总而言之,仍然是我见过的最好的解决方案。
  • 是的,我确实提交了错误报告:D 希望他们能尽快解决这个问题。
  • 是的。顺便说一句,我在一个 2D 应用程序中检查了结果,发现它不是白色的,它是透明的。所以我保存到jpeg,一切正常。顺便说一句,我需要这些——正如你可能已经猜到的——上传到 Google Play。如果我在 Google 上搜索某些手机的分辨率并以这些分辨率截取屏幕截图,这对 手机平板电脑 所需的屏幕截图有用吗?
猜你喜欢
  • 2014-01-29
  • 2012-12-30
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多