Game Development Reference
In-Depth Information
The
MAIN
scene file is designed to not only be the entry point of our game, but it
also contains the
_global
GameObject. This GameObject acts as the parent of a
hierarchy of objects that have persistent scope throughout the game. The following
GameObjects should have global scope:
•
MainCamera
: This is the camera to which the scene will be rendered
•
Player1
: This is the name of the GameObject that is the playable character
or the hero of the game
•
Score
: This is a
GUIText
element that displays the number of points the
player has acquired so far
Every other GameObject will exist in one of the three scene files for the
game—
LEVEL1
,
LEVEL2
, or
LEVEL3
. Using the
Applica-
tion.LoadLevelAdditive()
method, we can make sure each playable level
has both the objects from the persistent
_global
scene file and the level-specific
objects from the scene. This is because
Application.LoadLevelAdditive()
combines the GameObjects from the loaded scene with the GameObjects that are
already loaded. Contrast this with
Application.LoadLevel()
, which will des-
troy the previously instantiated scene file and all of its GameObjects before loading
the new scene, and the reason for loading additively is clear; we want both sets of
GameObjects to be loaded so we use the additive load!
Of course, we need to be absolutely sure that there are no duplicate objects in the
_global
scene file and the level-specific scene, or else logical errors will ensue.
Let's ensure we can do this now by updating the
GameMgr
script as follows:
1. In
GameMgr.cs
, ensure that the
eGameState
enumeration has entries for
Invalid
,
MainMenu
,
Level1
,
Level2
, and
Level3
. We will delegate re-
sponsibility for switching levels to this class instance. Each of these enumer-
ated values will correspond to the scene file with the same name. Through
this enumeration, we will inform the
GameMgr
script which scene file to load:
public enum eGameState
{
eGS_Invalid = -1, //enum for error case
eGS_MainMenu = 0,
eGS_Level1 = 1,
eGS_Level2 = 2,