Game Development Reference
In-Depth Information
the function Application.LoadLevel() is used. The name you give
to this function is the name you gave the scene when it was saved.
You can't reference another scene, however, if it is not included in
the build. To include a scene, when the scene is open, select File
Build Settings from the main menu. The window that opens has
an area at the top where all included scenes are listed. Press the
Add Scene button to put the scene into the list. The scene at the
top of the list is the one that will open first when your game is run
as an application. Drag and drop the scenes in the list to change
the order. Press the Build button at the bottom of the window
to create a stand-alone application. This includes an app for the
Mac and an exe for Windows. These are the files that you can then
distribute to others for playing.
Step 15. Modify the code in mainmenu.js to that in Listing 4.10 .
Listing 4.10 Setting Player Preferences with Menu Selection
function OnGUI ()
{
GUI.BeginGroup (Rect (Screen.width / 2 — 50,
Screen.height / 2 - 60 , 100, 120 ));
GUI.Box (Rect (0,0,100, 120 ), "Main Menu");
if(GUI.Button (Rect (10,40,80,30), "Play Level 1"))
{
PlayerPrefs.SetInt("Level", 1);
Application.LoadLevel("maingame");
}
if(GUI.Button (Rect (10,80,80,30), "Play Level 2"))
{
PlayerPrefs.SetInt("Level", 2);
Application.LoadLevel("maingame");
}
GUI.EndGroup ();
}
Step 16. The PlayerPrefs.SetInt() functions allow a system level
setting of a variable. In this case it is called Level and it is set to 1 or 2
depending on the button pressed. This makes the variable available to
be picked up by the maingame Scene when it opens. You can also use
PlayerPrefs to store highest scores and other player details that can
be used when the game is played the next time. To get the player's
level preference before the game starts, open spawn.js and modify the
code as shown in Listing 4.11 .
Search Nedrilad ::




Custom Search