Game Development Reference
In-Depth Information
Listing 8-5. Revised GameManager class to respond to menu button presses
01 //GameManager
02 //For high level control over game
03 //--------------------------------------------------------------
04 using UnityEngine;
05 using System.Collections;
06 //Game Manager requires other manager components
07 [RequireComponent (typeof (NotificationsManager))] //Component for sending and receiving notifications
08 //--------------------------------------------------------------
09 public class GameManager : MonoBehaviour
10 {
11 //--------------------------------------------------------------
12 //[Other GameManager code here...]
13 //--------------------------------------------------------------
14 // Use this for initialization
15 void Start ()
16 {
17 //Add cash collected listener to listen for win condition
18 Notifications.AddListener(this, "CashCollected");
19
20 //Add listeners for main menu
21 Notifications.AddListener(this, "RestartGame");
22 Notifications.AddListener(this, "ExitGame");
23 }
24 //--------------------------------------------------------------
25 //[Other GameManager code here...]
26 //--------------------------------------------------------------
27 //Restart Game
28 public void RestartGame()
29 {
30 //Load first level
31 Application.LoadLevel(0);
32 }
33 //--------------------------------------------------------------
34 //Exit Game
35 public void ExitGame()
36 {
37 Application.Quit();
38 }
39 //--------------------------------------------------------------
40 }
Line 31. The Application.LoadLevel method is used to reload the active level,
effectively restarting the game.
Line 37. Application.Quit is called to terminate the game. The code, as given
in line 37, only works when the game is running as a stand-alone application,
and not in the editor. For more information, see the following note.
Search WWH ::




Custom Search