Graphics Reference
In-Depth Information
PlayerPrefs.SetInt( "totalPlayers", 1 );
LoadLevel( singleGameStartScene );
The PlayerPrefs call saves out the total number of players for the game, which is there
only as a formality (it doesn't actually do anything or get called) in all but the example
game Interstellar Paranoids . As the game has a cooperative game mode, we use the total-
Players pref to decide which mode to start in. None of the other example games uses this
key, although you could utilize it if you add multiplayer modes later on.
The next line calls on a function called LoadLevel and passes in the name of the scene
to load. The variable singleGameStartScene is used, again a public string for easy editing
in the Unity editor Inspector window, to determine which scene to load for a single player
game. In this menu system, we also have a variable named coopGameStartScene that,
when used, will branch of and display a different layout (with the addition of a “Start
Co-op game” button) and will be used to launch a co-op game. It may well be that, in
future games, you launch both types of games via the same scene and opt to use the total-
Players pref key or another system like that, but the additional string is provided as a “just
in case” as well as a simple way to determine whether or not to show that co-op start but-
ton with a slightly different menu layout:
if(coopGameStartScene!="")
{
if(GUI.Button(new Rect(0, 250, 300,
40 ),"START CO-OP"))
{
PlayerPrefs.SetInt("totalPlayers",2);
LoadLevel( coopGameStartScene );
}
if(GUI.Button(new Rect(0, 300, 300, 40 ),"OPTIONS"))
{
ShowOptionsMenu();
}
} else {
if(GUI.Button(new Rect(0, 250, 300, 40 ),"OPTIONS"))
{
ShowOptionsMenu();
}
}
In the code above, we also added an options button to the menu group. If the options
button is pressed, a function named ShowOptionsMenu() is called. This function has a sin-
gle purpose, which is to set whichMenu to 1 (to display the options menu). This may make
you wonder why it even exists as a function when we could just as easily set whichMenu
in the button codes; the reason is simply to keep things neat and easy to extend later on.
Perhaps in future menu code, you may want to play a sound, start a screen transition, or play
an animation. Putting all those codes into the button detect can get messy and confusing,
making the script harder to debug, hence why the ShowOptionsMenu function exists.
Now that we have dealt with starting the game and changing to the options menu, all
that is left to do is show an exit button, end the group, and finish the case statement:
if(GUI.Button(new Rect(0, 400, 300, 40),"EXIT"))
{
Search WWH ::




Custom Search