- Home /
Texture2D.ReadPixels not working
I am trying to grab a texture as it is displayed onscreen so I can use it elsewhere. In my scene, I have a plane with a material shader that takes in a WebCamTexture and a Mask texture. the shader masks the WebCamTexture input to only show whats within the mask image, and also pixelates it.
I want to take a snapshot of what I see onscreen and apply it to a Sprite to use elsewhere.
I have tried using Graphics.Blit() and Texture2D.ReadPixels() to output (at this point any/all of the screen - though ideally I will have to select only a specific portion) the texture2D, but I keep getting a black image.
I got this code from another thread. It's currently what I'm working with:
// Initialize and render
RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);
cam.targetTexture = rt;
cam.Render();
RenderTexture.active = rt;
// Create Texture
Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
// Read pixels
tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
// Clean up
//cam.targetTexture = null;
//RenderTexture.active = null; // added to avoid errors
//DestroyImmediate(rt);
tex.Apply();
return tex;
Can anyone tell me why this isn't working?
Your answer
Follow this Question
Related Questions
Render Texture does not work on only IOS devices 0 Answers
scale squash rendertexture to different size 1 Answer
Why is ReadPixels on my RenderTexture creating a pure gray texture? 1 Answer
ReadPixels returns RGBA(0,0,0,0) 0 Answers
Can a Texture2D be created at runtime from a snapshot of a RenderTexture? 3 Answers