Game Development Reference
In-Depth Information
Add the following to the OnGUI function, below the // manage layout section:
9.
// Constrain all drawing to be within a 500x500 pixel area centered on the screen.
GUI.BeginGroup (new Rect (screenW / 2 - 250 , screenH - screenH/2 - 150, 500, 500));
// add controls here
// must match all BeginGroup calls with an EndGroup
GUI.EndGroup ();
The group placement for the X is straightforward; it centers the group using half the screen width.
The vertical positioning is a bit trickier. It references from the bottom of the screen, gets the halfway
value, and then adds an extra offset.
Rather than hard-coding the sizes and locations of the buttons, you will set up variables for
those values.
Add the button code below the // add controls here line:
10.
int ySize = 64; // button width
int xSize = 400; // button height
int y = 150 + yOffset * 3; // adjust the starting vertical location
according to screen size
int x = 250-xSize/2; // the horizontal location
// Play Game
if (GUI.Button( new Rect (x,y,xSize,ySize), "Play Game")) {
// start game
Application.LoadLevel("GardenLevel1");
}
y += ySize + yOffset;
// Settings
if (GUI.Button( new Rect (x,y,xSize,ySize), "Main Menu")) {
// go to settings menu
Application.LoadLevel("MainMenu");
}
y += ySize + yOffset;
// Quit
if (GUI.Button( new Rect (x,y,xSize,ySize), "Quit")) {
// quit application
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
11.
Save the script.
 
Search WWH ::




Custom Search