Game Development Reference
In-Depth Information
Listing 6.1 Script to Create a Three-Buttoned Main Menu
in the Center of the Display
function OnGUI ()
{
GUI.BeginGroup (
Rect (Screen.width / 2 — 50, Screen.height / 2 — 50,
100, 150));
GUI.Box (Rect (0,0,100,150), "Main Menu");
GUI.Button (Rect (10,40,80,30), "Play");
GUI.Button (Rect (10,70,80,30), "Help");
GUI.Button (Rect (10,100,80,30), "Quit");
GUI.EndGroup ();
}
Step 11. Attach the script to the Main Camera. Play. Note the menu
with the buttons. Currently the buttons do not do anything. Values
in the Rect() function inside each of the GUI items define the x , y
positions of the item and then the width and height, respectively.
For detailed definitions on all GUI items, see the Unity Manual at
http://unity3d.com/support/documentation/Manual/Game%20
Interface%20Elements.html .
Step 12. Before we can link the buttons to other scenes, we need
to create these scenes. From the main menu select File > New
Scene. Then select File > Save Scene and call it game . In the Project
you will now see two scenes (they have little unity icons next to
them): game and mainmenu . Whatever assets are in Project are
available for all scenes. The objects in each individual scene show
up in the Hierarchy. Now you are in the game scene; note that the
mainmenu code is no longer attached to the Main Camera. This
is because it is a different Main Camera. This one is in the game
scene.
Step 13. Create a new JavaScript file called gamescenemenu and add
the code in Listing 6.2 . Attach the script to the Main Camera.
Listing 6.2 Script to Set a Button to Load Another Level
function OnGUI()
{
if( GUI.Button(Rect(10, 10, 80, 30), "Back"))
{
Application.LoadLevel("mainmenu");
}
}
Search Nedrilad ::




Custom Search