Search Unity

Feature Request Get game view / simulator width and height from within OnDrawGizmos

Discussion in 'Editor & General Support' started by Xarbrough, Jan 7, 2021.

  1. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    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).
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    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.
     
  3. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    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.
     
  4. MW_MichaelWolf95

    MW_MichaelWolf95

    Joined:
    May 23, 2018
    Posts:
    3
    I'd also like to see this feature.
    Were you ever able to find a good work around?
     
  5. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I gave up on my implementation, but maybe today it would be possible to use the new Simulator API (starting in Unity 2021 I think).
     
  6. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    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.
     
  7. freakrho

    freakrho

    Joined:
    Aug 19, 2013
    Posts:
    9
    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
     
    Last edited: May 18, 2023
  8. OmarVector

    OmarVector

    Joined:
    Apr 18, 2018
    Posts:
    130
    I had the same issue and found this working out

    Camera.main.pixelHeight
    Camera.main.pixelWidth
     
  9. oleksiydemidenko

    oleksiydemidenko

    Joined:
    Jan 12, 2024
    Posts:
    1
    I used Camera.main.pixelHeight.

    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.