Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Middle of the screen on every device

Discussion in 'Scripting' started by herbie, Sep 11, 2013.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    A question just for sure.

    I have this code:
    Code (csharp):
    1. GUI.Label(Rect(Screen.width/2,Screen.height/2,100,30), String.Format("Test"), styleBox);
    Is it right that the GUI Label is displayed always in the middle of the screen?
    It does no matter on what device (android, iphone, pc)?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No; the label starts in the middle of the screen, but extends 100 pixels to the right and 30 pixels down. If you want it actually centered then you need to subtract 50 from the x and 15 from the y.

    --Eric
     
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Ok thanks.

    So when I have this for example:

    Code (csharp):
    1. GUI.Label(Rect(Screen.width/2-50+x,Screen.height/2-15+y,100,30), String.Format("Test"), styleBox);
    the middle of the GUI Label is always at x,y from the middle of the screen, on every device?
     
  4. devandart

    devandart

    Joined:
    Jun 7, 2013
    Posts:
    144
    Hi,

    that's right.
    But why don't you try it? :)

    Cheers.
     
  5. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks.

    Yes I've tried it but I was wondering if it's always true on every device.
    I have seen a lot of unexpected things with scripting.

    Now another question.
    When I place a GameObject (a cube for example) at (x,y,z) = (0,0,0), is that also always in the middle of the screen on every device?
    Or is the main camera always the middle of the screen?
     
  6. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    That's in world-space. The GUI works in screen-space. It will appear central to the camera if said camera is pointing directly at it.

    You may convert between them using:
    Code (csharp):
    1. camera.WorldToScreenPoint(worldPoint) and camera.ScreenToWorldPoint(screenPoint) .
     
    Last edited: Sep 11, 2013
  7. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Thanks.

    Actually my point is that I would like to have a GUI text exactly on a Gameobject for any kind of device.

    I have a 2D game, some kind of puzzle. When you start the game you see a playing board (something like a chess board). This board can not move.
    I want to place some GUI text on a specific place on the board.
    What I would like to have is that this text is always at the same place compared to the board. On every device with every resolution.
     
  8. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Like this...

    Code (csharp):
    1.  
    2. GameObject boardItem;
    3.  
    4. Vector3 screenPoint = camera.WorldToScreenPoint(boardItem.transform.position) ;
    5.  
    6. GUI.Label(Rect(screenPoint.x - 50, Screen.height - screenPoint.y - 15, 100, 30), String.Format("Test"), styleBox);
    7.