Take a screenshot without the UI elements in Unity

Viewed 1124

I am trying to capture what the camera sees in phone in an AR app and take a photo of it. What I found was to take a screenshot of the device and then save that as an image. However, I want to take a screenshot of what the camera sees instead of the screen. That is without any 2D or 3D elements created in the application. Just purely what the camera sees. How do I do this?

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;
    }

1 Answers

Interesting question.

What I would try is to set up a RenderTexture, and tell a camera in your scene to render to it. Then I would try to use the ImageConversion class to get my camera eye screenshot to a file.

This is just what my attempt would be. Not sure if that is the correct way, so providing that just in case it helps. I would be glad to know the outcome of your attempt in case you try it :)

Related