Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Screen,height reporting editor game window height

Discussion in 'Scripting' started by Eagle32, Sep 6, 2010.

  1. Eagle32

    Eagle32

    Joined:
    Jun 20, 2010
    Posts:
    89
    As per title. Screen.height and Screen.width report the dimensions of the Game window in the editor which contains the running game, not the dimensions of the area the game is actually playing in.

    This occurs if you use Screen.width/height from an editor script. This makes some sense; however, it presents a problem for me.

    The problem is that while running in the editor I want to be able to change values via my custom Inspector and have these changes affect the game. The problem is that when I make a change I need to tell my game to recalculate various things. These things are dependant on the Screen dimensions.

    One work around I've found has been to make the recalculation method a coroutine which yields immediately when called for one frame and then executes normally the next frame. If I do this when the coroutine executes the Screen dimensions are correct. I assume that after the editor classes have been executed the Screen dimensions get changed to the actual game dimensions for the non-editor behaviours. There are multiple different methods and scenarios that mean creating coroutine versions of all my methods is not a great solution.

    Another option I don't want to use is to poll all the variables of my component in one of its Update methods so that I can invoke the recalculation from there.

    Anyone got any other ideas? Is there a way of getting the actual game window dimensions rather than the containing window during OnInspectorGUI?
     
  2. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Sorry for the necro post, but I have a very similar issue. Almost 7 years to the day.

    Anyone one has a workaround?

    Thanks for any help
     
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    I think if you have a screen-space canvas, that stores the height/width no? Perhaps you can get it from the canvases dimensions.
     
  4. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    At the moment, I've just kept the result of width/height in two static variable at Awake. That's working. But maybe there's a less ugly trick.
     
  5. vr4kdQUe

    vr4kdQUe

    Joined:
    Jan 22, 2018
    Posts:
    1
    I'm replying to this post for the people who may have the same problem and come across this post.

    As OP stated, using 'Screen.width/height' inside 'OnInspectorGUI()' function will leave you with a size of 'Inspector', not 'Game' (current resolution). If you want the latter, then you will have to invoke them somewhere else.

    Example:
    Code (CSharp):
    1. [CustomEditor(typeof(pseudoScript)), CanEditMultipleObjects]
    2. public class pseudoScriptEditor : Editor
    3. {
    4.     float width, height;
    5.  
    6.     void OnEnable(){
    7.         width = Screen.width;
    8.         height = Screen.height;
    9.     }
    10.  
    11.     public override void OnInspectorGUI(){
    12.         ...
    13.         if (GUILayout.Button("Log", GUILayout.MaxWidth(50f))){
    14.             Debug.log(width);
    15.             Debug.log(height);
    16.         }
    17.         ...
    18.     }
    19. }
    Now you have it. If not, try to select a different GameObject momentarily and then try again.