Normally, calling Screen.width returns the width of the editor game view or simulated device screen width when using the simulator. However, when calling the same API from within OnDrawGizmos, the width of the current scene view window is returned.
I assume this implementation was chosen to make Gizmo drawing operations easier, however, it also makes it impossible for my Gizmo code to reconstruct the game view screen size if I need it. I’m looking for a solution that will return the correct size even when using the device simulator. This would make it possible to draw world space Gizmos that indicate information about the game screen window (e.g. view frustum with a pixel padding or when debugging safe area).
There is also Screen.safeArea to consider. As to other uses, I’m not sure I exactly grasp what you’re trying to do as I tend to just build for device directly, never really the simulator.
Thanks, I do know about Screen.safeArea and also use it, however I need Screen.width and Screen.height in my Gizmo code. However, Unity has a magic implementation where Screen.width returns different values depending on where it is called from. In regular code (e.g. from within Update) it will return either the size of the actual screen when running on device, or the size of the simulated screen in the Simulator or the size of the Game View. In OnDrawGizmos, Screen.width returns the width of the scene view window. My Gizmo code is used for debugging in the editor and should draw some things in the scene, which depend on the size of the game view.
I keep coming back to this issue. For example, now I need to calculate WorldToScreenPoint for a CinemachineVirtualCamera without having access to the regular Unity camera object. It all would have worked after 5 minutes, hadn’t I been distracted by “incorrect” values reported via Screen.width and Screen.height while trying to debug the function with Gizmos. It works fine when called in the Update loop, but it’s bonkers to change the implementation of an API depending on who calls it. How does Unity expect people to deal with this or even notice and learn about it? The new “it works on my machine” is now “it works when I call it somewhere in my specific call tree”.
And the answer still seems to be that it’s not possible to retrieve the actual gameplay screen with from OnDrawGizmos.
Screen.currentResolution gives you the correct resolution for the simulator, you can check if Application.isEditor is true to see if you are not using the simulator and use Screen.width and Screen.height instead
But this can be achieved by getting Screen.width and Screen.height inside Update() into a separate static variable which can then be used in OnDrawGizmos. Just wrap it in #if UNITY_EDITOR if possible.