Unity屏幕截图错误:也捕获了编辑器 [英] Unity screenshot error: capturing the editor too

查看:305
本文介绍了Unity屏幕截图错误:也捕获了编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create some screenshots but ScreenCapture.CaptureScreenshot actually captures the entire editor and not just the Game view.

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.");
        }
    }
}    

可能是什么问题?如何拍摄包括用户界面的体面的仅游戏视图的屏幕截图?

What could be the issue? How to take a decent, game view only screenshot that includes the UI?

注意,这是UI的东西,我在网上发现了其他方法来截屏(使用RenderTextures),但是这些方法不包括UI.在我也有UI的另一个真实"项目中,我只是打开了这个测试器项目,以查看屏幕快照问题是否在这里也仍然存在.

Note, the UI thing, I found other methods online to take a screenshot (using RenderTextures) but those didn't include the UI. In my other, "real" project I do have UI as well, I just opened this tester project to see if the screenshot issue persists here too.

推荐答案

这是一个错误,建议您在ScreenCapture.CaptureScreenshot足够成熟之前不要使用它.此功能已在Unity 2017.2 beta中添加,因此现在是从编辑器提交错误报告的正确时间.更糟糕的是,它只在我的计算机上保存黑白图像.

This is a bug and I suggest you stay away from it for while until ScreenCapture.CaptureScreenshot is mature enough. This function was added in Unity 2017.2 beta so this is the right time to file for a bug report from the Editor. To make it worse, it saves only black and blank image on my computer.

对于截屏,还有其他方法可以不使用RenderTextures,也可以在截屏中包含UI.

As for taking screenshots, there other ways to do this without RenderTextures, that will also include the UI in the screenshot too.

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

You can read pixels from the screen with Texture2D.ReadPixels then save it with 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);
}

这篇关于Unity屏幕截图错误:也捕获了编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆