Search Unity

Screen size at start not equal to GUI update size

Discussion in 'Scripting' started by Holynub, Nov 16, 2010.

  1. Holynub

    Holynub

    Joined:
    Nov 9, 2010
    Posts:
    60
    Hey everyone,

    I'm not sure if this is just a random bug on my part or if it's happened to anyone else.

    I'm trying to setup a GUI and positioning elements is becoming a pain. I wanted to initialize their positions in the Start() function. Elements are positioned according to the size of the screen. For example I'd use the following to try and get something positioned at the center of the screen.

    Code (csharp):
    1.     Pos.x = Screen.width/2 - dialTex.width;
    2.     Pos.y = Screen.height/2 - dialTex.height;
    3.  
    4.  
    as a debug i'd print the x and y value of Pos as well as the values of Screen.width and Screen.height and they are consistent. The crap hits the fan when I hit the OnGUI function. I put a debug line into it and the Screen.width and Screen.height are completely different from the start value. Should I just initialize the screen variables in the OnGUI call? Or am I just doing it wrong?
     
    Last edited: Nov 16, 2010
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I assume you're using Unity 3, which has a bug where using maximize on play returns the wrong results for Screen.width/height for the first couple of frames. You can either not use maximize on play, wait for a bugfix, or delay setting any values based on Screen.width/height for a few frames after start.

    --Eric
     
  3. Holynub

    Holynub

    Joined:
    Nov 9, 2010
    Posts:
    60
    Thx, I'll just initialize it on the first OnGUI call.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I don't think that will work either. You have to wait a few frames.

    --Eric
     
  5. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    Or make sure you have a game view tab open and active when you hit play. Then sizes are reported correctly.
     
  6. GeorgeDolbier

    GeorgeDolbier

    Joined:
    Nov 15, 2010
    Posts:
    2
    Confirmed that works. I tried it out with Eric5h5's Vectrosity demo, I couldn't hardly get any of them to work in the Scene view until I started running the scene with the "game" tab open. Gaaa buggers. Thanks for the work around DocSwab
     
  7. Bren

    Bren

    Joined:
    Aug 28, 2008
    Posts:
    191
    Clarification: This workaround requires that the Game window be open and at full size. If there is any shrinking of the Game window in the editor prior to running with Maximize on Play checked, the sizes are reported in correctly.
     
  8. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    If you aren't using Maximize on Play, you will also see the same problem if you don't have the Game View active before you hit Play. (at least I do!)