Unity Camear.Render() UI Mask invalid

If renderTexture has image with ui mask, the mask effect on png invalid


game show:


camera.Render show:

Here is my camer render code:

    public void CaptureCamera (Camera camera, Rect rect)
        {
            RenderTexture rt = new RenderTexture ((int)rect.width, (int)rect.height, 0);
            camera.targetTexture = rt;
            camera.Render ();
            RenderTexture.active = rt;
            Texture2D screenShot = new Texture2D ((int)rect.width, (int)rect.height, TextureFormat.ARGB32, false);
            screenShot.ReadPixels (rect, 0, 0);
            screenShot.Apply ();
            camera.targetTexture = null;
            RenderTexture.active = null; // JC: added to avoid errors
            GameObject.Destroy (rt);
   
            byte[] bytes = screenShot.EncodeToPNG ();
            string path = FilePath.GetWriteAblePath () + "/abc.png";
            System.IO.File.WriteAllBytes (path, bytes);
        }

In case someone comes across this topic, the following change should fix the problem:

RenderTexture rt = new RenderTexture ((int)rect.width, (int)rect.height, 24);

2 Likes

You saved my day!!!