- Home /
Texture.Apply() Crashes at Resolutions under 1920x1080
So, this is a bug that is, I must admit, "weird as hell". I have a system set up for taking a screenshot of the scene.
My code is very small, it is these few lines:
Texture2D result;
result = new Texture2D(Mathf.RoundToInt(captureZone.width) + destX,
Mathf.RoundToInt(captureZone.height) + destY,
TextureFormat.RGB24, false);
result.ReadPixels(captureZone, destX, destY, false);
result.Apply();
return result;
Now, I'm using this to apply an effect on my screen when I pause the game. I'm capturing the screen once when pause is pressed, which is fine. I then draw that captured screen with a blur effect using Graphics.DrawTexture at a quarter its original scale. I then capture this into another texture, and draw it again to the entire screen, with the same blur effect applied again (this had minimal performance impact and allowed me to apply another blur). I then attempt to capture this last image one last time, so I can draw it directly to the screen without having to apply the filter every frame.
It is this last capture that causes problems. It is using exactly the same code as the first capture (namely, running the above code with the captureZone set to a rectangle at (0,0), spanning to Screen.width and Screen.height, and with destX and destY both set to 0. This works fine in the editor, and when I run the external build at a resolution of 1920x1080.
If I run it at any other resolution, however (regardless of aspect ratio), it immediately freezes. I have probed as much as I can and have found that the line:
result.Apply();
is the one that is freezing the application. The build's log does not display any error messages.
Run on another computer, however, it all seems to work. I am somewhat concerned by this issue, as it seems completely nonsensical, and I have no clue what could be causing it. If anyone has any ideas, please don't hesitate to let me know!
I've shuffled around the code to remove this last Apply call, and it still crashes, though now it does so in a very weird way. It seems it's calling the draw functions exactly as expected (it prints to the output log every frame if I stick a print statement in there), but it still hangs. Also, it seems that I can't run the game in full screen at all, as it resizes the display, then hangs as a background process, without even running the splash screen. This might suggest it's an issue with the player, rather than with my code.
Another thing that's interesting to note is that I have multiple graphics processors on my PC, and I can explicitly run a program with a specific processor. When I do this (to ensure it is running on the right processor), it appears to use the wrong data set, and runs an older build. Not sure why this would be, either, but again might suggest a Unity issue.
I managed to get it to run in full screen by moving it to a different folder from the rest of the builds. This also solved the specific processor problem. The crash, however, still remains a problem.
Further news! I have tried disabling DX3D11, which has stopped it from crashing. However, it now simply does not work, and displays either a blank texture, or random noise.
Update here: It still draws correctly at 1920x1080 resolution, but nothing else.
Answer by Hoeloe · Mar 24, 2014 at 02:00 PM
I fixed it! Not sure what the original bug was, but after shuffling it around a bit, I discovered that the issue was attempting to read a rectangle from the screen that had non-integer width and/or height. A simple call to RoundToInt fixed it perfectly.
Your answer
Follow this Question
Related Questions
Using Screenshot as Texture 4 Answers
Application.CaptureScreenshot on a Texture2D? 1 Answer
What's wrong with my screenshot function? 1 Answer
How to get banner ad's screenshot to a texture? 0 Answers
ReadPixels returns RGBA(0,0,0,0) 0 Answers