Game Development Reference
In-Depth Information
120 //Show level complete sign
121 GameObject MissionCompleteObject = GameObject.Find ("spr_mission_complete");
122 MissionCompleteObject.GetComponent<SpriteRenderer>().enabled=true;
123 }
124 //--------------------------------------------------------------
125 //Restart Game
126 public void RestartGame()
127 {
128 //Load first level
129 Application.LoadLevel(0);
130 }
131 //--------------------------------------------------------------
132 //Exit Game
133 public void ExitGame()
134 {
135 Application.Quit();
136 }
137 //--------------------------------------------------------------
138 //Save Game
139
public void SaveGame()
140 {
141 //Call save game functionality
142
StateManager.Save(Application.persistentDataPath + "/SaveGame.xml");
143 }
144 //--------------------------------------------------------------
145 //Load Game
146
public void LoadGame()
147 {
148 //Set load on restart
149 bShouldLoad=true;
150
151 //Restart Level
152 RestartGame();
153 }
154 //--------------------------------------------------------------
155 }
Lines 39 and 68.
The
LoadSaveManager
is added as a private member and public
property of the
GameManager
class. Therefore, through the GameManager, each
and every other class has access to the current game state, as well as access to
loading and saving games directly, through the
SaveGame
and
LoadGame
functions
implemented further down in the GameManager.
Line 87.
The GameManager is created with the
DontDestroyOnLoad
property
set to true, meaning the object will persist across scenes and when the current
scene is reloaded. This integrates with the
LoadGame
function at line 146, in
which the level is first
reset
to its default, and then loaded from an XML file.