首页 > 解决方案 > 在 Unity 中截取没有 UI 元素的屏幕截图

问题描述

我正在尝试在 AR 应用程序中捕捉相机在手机中看到的内容并拍照。我发现是截取设备的屏幕截图,然后将其保存为图像。但是,我想截取相机看到的而不是屏幕的屏幕截图。即没有在应用程序中创建任何 2D 或 3D 元素。纯粹是相机所看到的。我该怎么做呢?

public void Start() {
        StartCoroutine ("SaveImage");
}

WaitForEndOfFrame frameEnd = new WaitForEndOfFrame ();

    IEnumerator SaveImage() {
        // Create a texture the size of the screen, RGB24 format
        int width = Screen.width;
        int height = Screen.height;
        yield return frameEnd;
        var tex = new Texture2D (width, height, TextureFormat.RGB24, false);

        // Read screen contents into the texture
        tex.ReadPixels (new Rect (0, 0, width, height), 0, 0);
        tex.Apply ();

        byte[] bytes = tex.EncodeToPNG ();
        Destroy (tex);
        var form = new WWWForm ();
        form.AddField ("plant", plantComponentID);
        form.AddBinaryData ("image", bytes, "screenShot.png", "image/png");

        yield return null;
    }

标签: c#unity3d

解决方案


有趣的问题。

我会尝试设置一个RenderTexture,并告诉场景中的相机对其进行渲染。然后我会尝试使用 ImageConversion类将我的相机眼睛截图保存到文件中。

这正是我的尝试。不确定这是否是正确的方法,所以提供它以防万一。如果您尝试一下,我很高兴知道您尝试的结果:)


推荐阅读