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

Centre GUI.box

Discussion in 'Immediate Mode GUI (IMGUI)' started by Fersutagames_, Nov 2, 2013.

  1. Fersutagames_

    Fersutagames_

    Joined:
    May 2, 2013
    Posts:
    52
    Hello all!

    My question today is, how can i center the GUI? The code ive attached below centers it, but the GUI box changes size a lot. EG:

    in the editor say it will look like this | Press E To open crafting bench |
    But in the compiled project the text will be centred but the GUI.box will be too big.

    Which looks kinda odd when id prefer it to stay small. Any ideas?

    Oh and heres the code;

    Code (csharp):
    1.  var hasCollided : boolean = false;
    2. var labelText : String = "";
    3.  
    4. function OnGUI()
    5.     {
    6.        if (hasCollided ==true)
    7.     {  
    8.       GUI.Box(Rect(140,Screen.height-50,Screen.width-300,120),(labelText));
    9.     }
    10. }
    11.  
    12. function OnTriggerEnter(c:Collider)
    13.     {
    14.        if(c.gameObject.tag =="Player")
    15.  
    16.     {
    17.  
    18.        hasCollided = true;
    19.        labelText = "Press E to open workshop";
    20.  
    21. }
    22. }
    23.  
    24. function OnTriggerExit( other : Collider ){
    25.     hasCollided = false;
    26.  
    27. }
     
  2. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
  3. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    I think there's something not quite right with your code if the box is changing size. Try this to center your Box :

    Code (csharp):
    1.  
    2. GUI.Box(Rect (Screen.width / 2 - 50, Screen.height / 2 - 50, 100, 100));
    3.  
    And I'd Suggest that you use GUI.Label to display labelText. It would work better.
     
  4. Fersutagames_

    Fersutagames_

    Joined:
    May 2, 2013
    Posts:
    52
    Thank you both! Dkozar ill look into merging my code c:

    and intense_gamer that worked too! c:
     
  5. GamerYou

    GamerYou

    Joined:
    Jun 13, 2013
    Posts:
    19
    Hi,

    You can do it easily with GUI Design HTML CSS