x


GUI Screen Size

I have this script below and it works perfectly with unity's maximized viewer. What would the script be to get it to look exactly like this for any window size?

function OnGUI () { // Make a background box GUI.Box (Rect (560,220,180,250), "Menu");

 // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
 if (GUI.Button (Rect (570,250,160,40), "Start")) {
     Application.LoadLevel ("Start");
 }
 if (GUI.Button (Rect (570,300,160,40), "How to Play")) {
     Application.LoadLevel ("oldcity");
 }
     if (GUI.Button (Rect (570,350,160,40), "Options")) {
     Application.LoadLevel ("IDKYET");
 }
     }

more ▼

asked Feb 26, 2012 at 09:40 PM

sketchers1 gravatar image

sketchers1
229 57 58 66

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

use this

/ Make the first button. If it is pressed, Application.Loadlevel (1) will be executed

if (GUI.Button(Rect( 35*(Screen.width)/100, 27*(Screen.height)/100, 24*(Screen.width)/100,26*(Screen.height)/100),"Start"))

{

Application.LoadLevel ("Start");

}

if (GUI.Button(Rect( 35*(Screen.width)/100, 27*(Screen.height)/100, 24*(Screen.width)/100,26*(Screen.height)/100),"How To play"))

{

Application.LoadLevel ("oldcity");

}

if (GUI.Button(Rect( 35*(Screen.width)/100, 27*(Screen.height)/100, 24* (Screen.width)/100,26*(Screen.height)/100),"options"))

{

Application.LoadLevel ("IDKYET");

}

more ▼

answered Feb 28, 2012 at 06:13 PM

James Tima gravatar image

James Tima
21 1

(comments are locked)
10|3000 characters needed characters left

You have to think of everything in terms of 0-1 percents, and then multiply by Screen.width and height.

For example, suppose the Start button was currently L/R centered and 1/4th of a screen wide (so left-corner is at 3/8ths = 0.375.) The button would use:

 Rect( 0.375f * Screen.width, ___ , 0.25f*Screen.width, ___ );
       ^^ start at 3/8ths ^^        ^^ 25% wide ^^^^^^

If you just wanted it centered and big enough to hold the text, still use 0.5f*Screen.width to find the center, then use your GUIStyle and MeasureFont and subtract half that many pixels to get the left edge.

Also may have problems that the Unity things that tell you 0-1 screen pos (like Camera.worldtoScreen) go from the botton, but GUI-dot commands go from the top, so may need to use use 1-percent or Screen.height - pix.

Not as bad as it sounds -- you get used to it quicky.

more ▼

answered Feb 26, 2012 at 10:01 PM

Owen Reynolds gravatar image

Owen Reynolds
22.9k 3 16 77

that is really confusing. it kind of makes sense.... how would I script it so that it ends up looking exactly how i described above? Thanks!!!!

Feb 27, 2012 at 12:49 AM sketchers1

Figure out what percent of the screen 570 currently is. You could just Debug.Log the screen size and hand-divide.

Feb 27, 2012 at 04:26 AM Owen Reynolds

thanks I got it to work:

function OnGUI () { // Make a background box GUI.Box (Rect(Screen.width/2.235f,Screen.height/2.6f,Screen.width/7.9f,Screen.height/2.4), "Menu");

 // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
 if (GUI.Button (Rect(Screen.width/2.225f,Screen.height/2.3f,Screen.width/8.2f,Screen.height/12.2f), "Start")) {
     Application.LoadLevel ("Start");
 }
 if (GUI.Button (Rect(Screen.width/2.225f,Screen.height/1.9f,Screen.width/8.2f,Screen.height/12.2f), "How to Play")) {
     Application.LoadLevel ("oldcity");
 }
     if (GUI.Button (Rect(Screen.width/2.225f,Screen.height/1.6f,Screen.width/8.2f,Screen.height/12.2f), "Options")) {
     Application.LoadLevel ("IDKYET");
 }
     }
Feb 28, 2012 at 02:59 AM sketchers1

Division by 2.225 doesn't seem that intuitive. Multiplying by 0.45 is the same thing, and is more obvious that you are saying "45% of a screen."

Feb 28, 2012 at 03:08 AM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left

If I am not mistaken, I believe you are looking for Screen.width and Screen.height.

more ▼

answered Feb 26, 2012 at 09:56 PM

OrangeLightning gravatar image

OrangeLightning
6.6k 127 117 173

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1

asked: Feb 26, 2012 at 09:40 PM

Seen: 2532 times

Last Updated: Feb 28, 2012 at 06:13 PM