简体   繁体   English

使用Unity和ARKit的屏幕截图

[英]Screen Captures Using Unity and ARKit

I'm taking a screenshot in a Unity application using 我正在使用以下内容在Unity应用程序中截屏

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

But the AR background being rendered by ARKit ends up being completely black with other GameObjects rendering correctly (floating in black space). 但是,ARKit渲染的AR背景最终与其他GameObjects正确渲染(漂浮在黑色空间中)完全是黑色的。

Have others encountered this issue? 其他人遇到过这个问题吗? Is there a known workaround? 有已知的解决方法吗?

There are too many bugs with ScreenCapture.CaptureScreenshot . ScreenCapture.CaptureScreenshot有太多错误。 Do not use this function to preform any screenshot in Unity at this moment. 目前,请勿使用此功能在Unity中执行任何屏幕截图。 It's not just on iOS, there are also bug on with this function in the Editor. 它不仅在iOS上,而且在编辑器中也存在此功能的错误。

Here is a remake of that function that can take screenshot in png, jpeg or exr format. 这是该功能的重制版,可以采用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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM