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. Dismiss Notice

[C#] How to make GUI position change based on screen resolution? (Please Help)

Discussion in 'Scripting' started by KyleStank, May 7, 2014.

  1. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    I am creating a very simple game. It is my first game by the way. It is for PC/Mac/Linux. I have scripted most of it without trouble. But my problem that I decided to fix/deal with last is the GUI auto scaling. I have created A LOT of GUI throughout my game. I have used, GUI.Label, GUI.Button, GUI Text, a lot of GUI, you should get the point. My problem is, when I enter "Free Aspect" mode in the editor, everything is where it should be. When I change the aspect in the editor or build the game and play at any resolution, nothing is where I want it to be EXACTLY although on one resolution, things are close to where they should be. How can I fix this to where the GUI will adjust to whatever screen resolution the user's screen is? Someone PLEASE help! I have been trying to fix this throughout the whole time I have been building my game. I saw something about using GUI.Matrix but I have NO idea what that is. Please help!
     
  2. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29
    Code (csharp):
    1.  
    2.     void OnGUI()
    3.     {
    4.         GUI.matrix = Matrix4x4.TRS( Vector3.zero, Quaternion.identity, new Vector3( Screen.width / 1980.0f, Screen.height / 1080.0f, 1.0f ) );
    5.  
    6.         GUI.Label (new Rect (50, 0, 50, 25), "Test");
    7.  
    8.     }
    9. }
    10.  
    Test this line of code and try this.

    the 1980 x 1020 is what I use for the default screen rez you would change these value to yours.
     
  3. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    Ok, I changed the numbers that you told me to change and the GUI.Label that you put in worked fantastic! But, on the same script, I have some buttons made. I made a custom aspect ratio of like 1:2 and the buttons still appeared but they were not in the EXACT spot. Is this normal? And another question is say someone who is playing the game doesn't have a screen of 1980 x 1080 or 1280 x 720 or 1600 x 900 or any resolution that I set it to, will the GUI go off their screen but not people who DO have the resolution that I made it. But this REALLY helps. WAY better on EVERYTHING compared to what it was...
    But if you can, please answer these 2 questions! Thank you!
     
  4. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    use some math combined with Screen.currentResolution.height and Screen.currentResolution.width, make up for the desired position of the menu's in your game.
     
  5. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29

    Could you post your code?

    I won't be able to look at it for about 12 hours though, I am leaving now for work.
     
  6. NIkkIWk369

    NIkkIWk369

    Joined:
    Dec 27, 2013
    Posts:
    3
    This littel chunk of code will return you a rectangle based on your current screen resolution... Most GUI elements use rectangles for size position etc. therefore it should solve your problem the easiest way:
    Code (csharp):
    1. Rect screenRelativeRect(float left, float top, float width, float height) {
    2.     Rect r = new Rect(Screen.width * left, Screen.height * top, Screen.width * width, Screen.height * height);
    3.     return r;
    4. }
     
  7. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    Here is my code:
    Code (csharp):
    1.  
    2. void OnGUI ()
    3.     {
    4.         GUI.matrix = Matrix4x4.TRS( Vector3.zero, Quaternion.identity, new Vector3( Screen.width / 1600.0f, Screen.height / 900.0f, 1.0f ) );
    5.         GUI.Label (new Rect (500, 500, 50, 25), "Test", customGUIStyle);
    6.         if (deleteSave == false)
    7.         {
    8.             //Draws Level Selecter Button
    9.             if (GUI.Button(new Rect(730, 200, 150, 65), "Select Level"))
    10.             {
    11.                 Application.LoadLevel("Level Selecter");
    12.             }
    13.  
    14.             //Draws the Delete Game Save Button
    15.             if (GUI.Button(new Rect(730, 300, 150, 65), "Delete Save"))
    16.             {
    17.                 deleteSave = true;
    18.             }
    19.  
    20.             //Draws the Game Shop Button
    21.             if (GUI.Button(new Rect(730, 400, 150, 65), "The Dumbest Store"))
    22.             {
    23.                 Application.LoadLevel("Game Shop");
    24.             }
    25.             if (GUI.Button(new Rect(730, 500, 150, 65), "Quit Game"))
    26.             {
    27.                 Application.Quit();
    28.             }
    29.         }
    30.         if (deleteSave == true)
    31.         {
    32.             GUI.Box(new Rect(50, 10, 1500, 700), "Are you sure your want to delete you game save?" + "\n" + "This will delete EVERYTHING you have ever done on this game so far!", customGUIStyle);
    33.             if (GUI.Button(new Rect(800, 580, 275, 70), "Confirm"))
    34.             {
    35.                 if (File.Exists("C:/Vlentful Studios/The World's Dumbest Game/" + "save.wdg"))
    36.                 {
    37.                     File.Delete("C:/Vlentful Studios/The World's Dumbest Game/" + "save.wdg");
    38.                     PlayerPrefs.DeleteAll();
    39.                     deleteSave = false;
    40.                 }
    41.                 else if (!File.Exists("C:/Vlentful Studios/The World's Dumbest Game/" + "save.wdg"))
    42.                 {
    43.                     deleteSave = false;
    44.                 }
    45.             }
    46.             else if (GUI.Button(new Rect(475, 580, 275, 70), "Cancel"))
    47.             {
    48.                 deleteSave = false;
    49.             }
    50.         }
    51.     }
    One problem is I would have to experiment with Screen.currentResolution because I have never worked with it. I will try to work with it though...

    My only question is does this HAVE to be in a method?(most people call it a function but I think method is the proper term in C#. That is what I read from my C# programming book) Because I have more than one button so I would think that I would just make more variables. And does saying the "(Screen.width * left)" mean LITERALLY tell it "left" or give a a number for the left that your want the button to be? Just clarifying.

    EDIT:
    I just now realized that I was using (Screen.width / 2) and that was messing things up. The code I posted is the right code. I forgot that I fixed it because I just played some basketball and forgot when I cam back. My bad. But I feel that something bad may happen soon. I will post here if something does go wrong.(again)
     
    Last edited: May 7, 2014
  8. Graveyard-Gamers

    Graveyard-Gamers

    Joined:
    Apr 14, 2014
    Posts:
    29

    yes using screen.width ./2 will get messed up using that bit of code I gave u. Hope everything is working now!