- Home /
ReadPixels problem
Hello, I have a problem that is driving me crazy , I searched literature about it but it seems no one gives to the solution , some say even that may be a bug in this version of Unity . The problem is that when I try to take a screenshot I obtain always an offset image. I used this code:
public IEnumerator CaptureCamera() { isProcessing = true; // wait for graphics to render yield return new WaitForEndOfFrame();
// create the texture
Texture2D screenTexture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
// put buffer into texture
screenTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
// apply
screenTexture.Apply();
dataToSave = screenTexture.EncodeToPNG();
destination = Path.Combine(Application.persistentDataPath, System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");
File.WriteAllBytes(destination, dataToSave);
isProcessing = false;
}
this the original img: and this is the result: My Unity version is 5.2.2f1 Personal Please someone knows the solution to this problem? Thanks in advance
Hi, do you have multiple render target/textures ? ReadPixels reads from the current render target so you may be saving an image from another render target/texture. Try setting RenderTexture.active to the correct RenderTexture before calling readpixels (I think setting it to null means you want to read what's on screen, not in a render texture).
O$$anonymous$$, is a Bug. I change for Unity version 5.1 and all works fine. Thanks anyway
I've just found this issue as well in 5.2.2p2. I'm only getting half a screenshot in portrait with the first half being black.
Did you find/report the bug so I can vote on it? Can't seem to find any mention in the issue tracker?
Welp, I'm having this problem too with Unity 5.2.3f1
Answer by wibble82 · Dec 07, 2015 at 02:20 PM
Could I just check - are you running the code in 'PostRender'. I could be wrong, but I believe that exactly where you call ReadPixels can matter (perhaps it shouldn't, but it might well do), as it's reading from a render target.
Try putting your code in 'OnPostRender' of your camera object.
-Chris