Search Unity

Re size GUI.box and GUI.Button

Discussion in 'Immediate Mode GUI (IMGUI)' started by Graveyard-Gamers, Apr 15, 2014.

  1. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    This is my code that I wrote. My question is How do I get it it re size when I change off of Free Aspect. For example 1024 X 768 shows Like how I have it set up in the scene. but when I change to 1920 X 1080 All my GUI is off center. I have done a google search but so far I have not been able to get anything to work. If you have a link that will help me I can research it on my own I have not issue with that. \

    Also I have many topics on how to re scale a texture that is attached to a object. But this is not my case. I am drawing my GUI from code. As of now the only way I see the GUI in my game window is when I hit play.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyGUI : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.     // Void OnGUI() is the function for your Graphic user Interface (GUI)
    16.     void OnGUI(){
    17.  
    18.         // Make a backgroud for the button.
    19.         // GUI.box = A box is created / new Rect() = A new Rect is created / new Rect(10(top left), 10(top right), 100(width), 100(height)) In pixels.
    20.         // "Main Menu" is what will be displayed at top of the box.
    21.  
    22.         GUI.Box (new Rect(460, 10, 100, 200), "Main Menu");
    23.  
    24.         // Make a button that can be clicked on.
    25.         // Debug.Log() creates text in your console.
    26.         // Application.LoadLevel(); Takes you to what ever scene that you tell it to. scene number goes in () after LoadLevel.
    27.  
    28.         if (GUI.Button (new Rect (470, 55, 80, 20), "Options")) {
    29.             Debug.Log ("Options");
    30.             //Application.LoadLevel(1);
    31.             }
    32.         if (GUI.Button (new Rect (470, 75, 80, 20), "Credits")) {
    33.             Debug.Log ("Credits");
    34.             //Application.LoadLevel(1);
    35.         }
    36.         if (GUI.Button (new Rect (470, 95, 80, 20), "Exit")) {
    37.             Debug.Log ("Exit");
    38.             //Application.LoadLevel(1);
    39.         }
    40.         if (GUI.Button (new Rect (470, 35, 80, 20), "New Game")) {
    41.             Debug.Log ("Loading New Game");
    42.             Application.LoadLevel(1);
    43.         }
    44.  
    45.  
    46.     }
    47. }
    48.  
     
    Last edited: Apr 15, 2014
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    This should help :
    Code (csharp):
    1.  
    2. function OnGUI () {
    3. GUI.Button (Rect (0,0,100,50), "Top-left");
    4. GUI.Button (Rect (Screen.width - 100,0,100,50), "Top-right");
    5. GUI.Button(Rect(Screen.width /2 - 50, Screen.height/2 - 50,  100, 50),"Center");
    6. GUI.Button (Rect (0,Screen.height - 50,100,50), "Bottom-left");
    7. GUI.Button (Rect (Screen.width - 100,Screen.height - 50,100,50), "Bottom-right");
    8. }
    9.  
     
    Last edited: Apr 15, 2014
  3. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Will this work in C#?
     
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Yeah, you'll just have to tweak it a little :

    Code (csharp):
    1.  
    2. void OnGUI () {
    3.  
    4. GUI.Button (new Rect (0,0,100,50), "Top-left");
    5.  
    6. GUI.Button (new Rect (Screen.width - 100,0,100,50), "Top-right");
    7.  
    8. GUI.Button(new Rect(Screen.width /2 - 50, Screen.height/2 - 50,  100, 50),"Center");
    9.  
    10. GUI.Button (new Rect (0,Screen.height - 50,100,50), "Bottom-left");
    11.  
    12. GUI.Button (new Rect (Screen.width - 100,Screen.height - 50,100,50), "Bottom-right");
    13.  
    14. }
    15.  
     
  5. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Ok I created this Script and I see buttons at each corner of my Screen. They also stay that way when I change my Screen size. But I am not sure I understand why this is. Then only difference i can see is your doing Screen.width /2 in two of the lines of code. Is this what keeps everything in the same spot?
     
    Last edited: Apr 15, 2014
  6. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Yeah. Using Screen.width Screen.height is more reliable than hard-coding the x y screen positions.
     
  7. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29

    Yep This helped a alot thank a ton.