Game Development Reference
In-Depth Information
GameManager and Singletons
The
GameManager
is a general, managerial and overarching class of special significance in practically
every game. It has the single duty of representing and coordinating all high-level functionality,
including game restarts, game exits, load-and-save states, game pauses, and more. It's notable
here, too, that I've referred to GameManager in terms of “the GameManager
”
(singular)—as in the
one and only
GameManager—as opposed to “a GameManager
”
(where the possibility of
multiple
instances is admitted). This is for good reason because, in general, we'll
never need
more than
one instance of GameManager throughout the duration of gameplay. The gamer can only play
one instance
of our game at any time, and that active instance is represented entirely by a single
GameManager
,
which is created at game-start and is terminated at game-end
.
Allowing for multiple GameManager instances would be confusing and game-breaking, since
multiple instances would necessarily conflict and fight for controlling the same game. Therefore, we
can safely establish here that not only will we never need more than one GameManager instance
at any one time, but there's also good reason to create the class so that multiple instantiations of it
are not possible. This will be especially useful if other programmers should work on our code. This
kind of object, where only one instance can be made, is known as a Singleton. Classes designed
to produce Singleton objects are said to use the
Singleton design pattern
. Thus, our
GameManager
object should be a Singleton. But how can we create such an object?
There are multiple solutions or methods for creating Singletons. The method illustrated here will be
through
static members
. Let's see this process, step by step (see Listing 4-13).
Listing 4-13. Starting the Singleton Class
01 //--------------------------------------------------------------
02 using UnityEngine;
03 using System.Collections;
04 //--------------------------------------------------------------
05 public class GameManager : MonoBehaviour
06 {
07 //Internal reference to single active instance of object - for singleton behaviour
08
private static GameManager instance = null;
09 }
Start by adding a
private static
member of type
GameManager
(line 08 in Listing 4-13). Being static,
the value of this member
would
be shared across
all
instances of
GameManager
. This variable will be
null
if there's no valid instance of
GameManager
active in the scene; otherwise, it'll be a reference to a
previously declared instance of
GameManager
. Next, see Listing 4-14.
Listing 4-14. Expanding on the Singleton Class
01 //--------------------------------------------------------------
02 using UnityEngine;
03 using System.Collections;
04 //--------------------------------------------------------------
05 public class GameManager : MonoBehaviour
06 {
07 //--------------------------------------------------------------
08 //C# property to retrieve currently active instance of object, if any