Game Development Reference
In-Depth Information
Singletons and managers
Any project of a sufficient size and complexity is going to run into issues related to man-
aging your game objects as and when they are added and removed from a scene. If you
don't get your design right from the start, you are setting yourself up for a world of mess
later. A common way to handle this is to use one of the three patterns, single instance
managers, singletons, or a dependency system, to manage these controllers for you.
There are two main ways through which you can implement the singleton pattern in Unity.
The first way is to use a public static parameter within a class to maintain the runtime
class. This also allows any other script to access it from anywhere in the game and is use-
ful if you want other events to cause the manager to do something, for example, things re-
lated to conversation systems or traps. You can also use an empty game object in the scene
and attach a singleton pattern script to it. However, you could cause conflicts if you add
more than one pattern.
Managers, on the other hand, are just central scripts that are particular to an individual
scene to control and maintain the flow of the scene for one or many items.
The manager approach - using empty game objects
Whereas singletons are game wide, there is often a cause for just a scene-based manager.
Implementing this using an empty game object is very easy. Simply use Create Empty
from the GameObject menu or the keyboard shortcut, as shown in the following screen-
shot. The placement of the new game object is up to you. If your controller's position is
important (like with an enemy spawner), place it where you want the objects to spawn
from. If not, it doesn't matter; it just needs to be in the scene somewhere. As it's an empty
game object, it will not be drawn.
Search WWH ::




Custom Search