屏幕捕获使用Unity和ARKit

时间:2017-10-17 17:28:02

标签: c# ios unity3d textures arkit

我正在使用

在Unity应用程序中截取屏幕截图

ScreenCapture.CaptureScreenshot ("screenshot.png", 2);

但ARKit渲染的AR背景最终会变成黑色,而其他GameObjects正确渲染(漂浮在黑色空间中)。

有其他人遇到过这个问题吗? 有没有已知的解决方法?

1 个答案:

答案 0 :(得分:2)

ScreenCapture.CaptureScreenshot存在太多错误。此时不要使用此功能在Unity中执行任何屏幕截图。它不仅仅在iOS上,编辑器中也存在此功能的错误。

以下是该功能的翻拍,可以以png,jpeg或exr格式截取屏幕截图。

IEnumerator CaptureScreenshot(string filename, ScreenshotFormat screenshotFormat)
{
    //Wait for end of frame
    yield return new WaitForEndOfFrame();

    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();

    string filePath = Path.Combine(Application.persistentDataPath, "images");
    byte[] imageBytes = null;

    //Convert to png/jpeg/exr
    if (screenshotFormat == ScreenshotFormat.PNG)
    {
        filePath = Path.Combine(filePath, filename + ".png");
        createDir(filePath);
        imageBytes = screenImage.EncodeToPNG();
    }
    else if (screenshotFormat == ScreenshotFormat.JPEG)
    {
        filePath = Path.Combine(filePath, filename + ".jpeg");
        createDir(filePath);
        imageBytes = screenImage.EncodeToJPG();
    }
    else if (screenshotFormat == ScreenshotFormat.EXR)
    {
        filePath = Path.Combine(filePath, filename + ".exr");
        createDir(filePath);
        imageBytes = screenImage.EncodeToEXR();
    }

    //Save image to file
    System.IO.File.WriteAllBytes(filePath, imageBytes);
    Debug.Log("Saved Data to: " + filePath.Replace("/", "\\"));
}

void createDir(string dir)
{
    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(dir)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(dir));
    }
}

public enum ScreenshotFormat
{
    PNG, JPEG, EXR
}

<强> USAGE

void Start()
{
    StartCoroutine(CaptureScreenshot("screenshot", ScreenshotFormat.PNG));
}