ReadPixels - Screen.Width/Height in not maximized editor mode give unreliable results
In my current project, I'm taking a screenshot of the current camera view via texture.ReadPixels(). Code:
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
texture.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height), 0, 0);
Everything is working fine all the time in maximized play inside the editor. When I'm logging Screen.Width() and Height it says 1920*1080 and everything is fine. However, when I'm not in the "Maximize On Play"-Mode I get the following error a lot:
[d3d11] attempting to ReadPixels outside of RenderTexture bounds! Reading (0, 0, 938, 420) from (937, 378)
Logging Screen.Width/Height tells me the resolution is 938 * 420. So I get why the exception is raised but I don't understand why Screen.Width/Height is giving a higher resolution back then what is actually displayed. Also from time to time I have to restart Unity and then everything works fine till I restart Unity again. Then the log says 1920*1080 even in not maximized mode.
Has anyone an idea how to solve this problem? It's really annoying to always test in maximized play-mode.
Having the same issue, did you ever find a way to solve this?
Your answer
Follow this Question
Related Questions
How to ReadPixels from square area right in the middle of the screen? 1 Answer
Unity3d Texture2D.ReadPixels problem when convert to sprite. 0 Answers
ReadPixels returns empty texture 0 Answers
Capturing high resolution screenshot with Temporal AA 1 Answer
Texture2D.ReadPixels doesn't capture image effects. 1 Answer