Game Development Reference
In-Depth Information
Implementing the GameMgr script
Let's implement the GameMgr script so that it can manage the loading (and future un-
loading) of scene files and assets. Implementing this in a flexible way now will make
our game more extensible when we have future levels to add. It can be implemented
by performing the following steps:
1. Recall that we have already created an empty script named GameMgr and at-
tached it to the Game GameObject. If you have not already done this, no wor-
ries; just create a new script now, and attach it.
2. In order for GameMgr to do its job, it will act as a mediator between
popupMenu and the scene files of the game. When GameMgr receives a mes-
sage to change its state, it will load and unload the appropriate scene files.
3. It is important that at this point we have added the LEVEL1 scene file to the
build settings; if you have not yet done this, make sure it has been added now.
4. We will use a custom enumeration in this class to build a state machine.
This is a data structure that will let us build a model of how all scene files in
the game interact with one another (which loads first, which stays persistent,
which loads next, and so on). For complex systems later in the game, we will
use this concept repeatedly.
5. We add an entry for both scene files we have created as well as a special error
value that we can use to trap potential data problems. Extending this enumer-
ation is as simple as adding more entries to this structure and assigning new
entries a new unique integer as shown in the following code:
public enum eGameState
{
eGS_Invalid = -1, //used to encode error
condition when
setting gamestate
eGS_MainMenu = 0,//a state to encode
being in the mainmenu
eGS_Level1 = 1//a state to encode being
in level1
}
6. For GameMgr to be able to detect when a state change occurs, we will require
two variables: one for the GameState and one for the GameState on the pre-
Search WWH ::




Custom Search