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");
}
}
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.
If I am not mistaken, I believe you are looking for Screen.width and Screen.height.
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”);
}