Singleton
[TOC]
Alternatives
Scriptable Objects
One excellent alternative to the singleton pattern in Unity is the use of ScriptableObjects as a type of global variable. Ryan Hipple from Schell Games gave a presentation at Unite Austin 2017 titled Game Architecture with Scriptable Objects that explains how to implement them and the many advantages over singletons.
Toolbox
The “Toolbox“ concept improves upon the singleton pattern further by offering a different approach that makes them more modular and improves testability.
Introduction
The singleton pattern is a way to ensure a class has only a single globally accessible instance available at all times. Behaving much like a regular static class but with some advantages. This is very useful for making global manager type classes that hold global variables and functions that many other classes need to access. However, the convenience of the pattern can easily lead to misuse and abuse and this has made it somewhat controversial with many critics considered it an anti-pattern that should be avoided. But like any design pattern, singletons can be very useful in certain situations and it’s ultimately up to the developer to decide if it’s right for them.
Advantages
- Globally accessible. No need to search for or maintain a reference to the class.
- Persistent data. Can be used to maintain data across scenes.
- Supports interfaces. Static classes can not implement interfaces.
- Supports inheritance. Static classes can not inherent for another class.
The advantage of using singletons in Unity, rather than static parameters and methods, is that static classes are lazy-loaded when they are first referenced, but must have an empty static constructor (or one is generated for you). This means it’s easier to mess up and break code if you’re not careful and know what you’re doing. As for using the Singleton Pattern, you automatically already do lots of neat stuff, such as creating them with a static initialization method and making them immutable.
Disadvantages
- Must use the Instance keyword (e.g.
.Instance) to access the singleton class. - There can only ever be one instance of the class active at a time.
- Tight connections. Modifying the singleton can easily break all other code that depends on it. Requiring a lot of refactoring.
- No polymorphism.
- Not very testable.
Implementation
The singleton pattern is commonly applied to multiple classes but the implementation is always the same. So creating a singleton base class others can inherit from is ideal because it eliminates the need to recreate the same code over and over again for each class that needs to be a singleton.
Notes:
- This script will not prevent non singleton constructors from being used in your derived classes. To prevent this, add a protected constructor to each derived class.
- When Unity quits it destroys objects in a random order and this can create issues for singletons. So we prevent access to the singleton instance when the application quits to prevent problems.
Singleton.cs
1 | using UnityEngine; |
Requirements
The above script makes use of the custom extension method GetOrAddComponent which is not a part of Unity.
Usage
To make any class a singleton, simply inherit from the Singleton base class instead of MonoBehaviour, like so:
1 | public class MySingleton : Singleton<MySingleton> |
Now you can access all public fields, properties and methods from the class anywhere using <ClassName>.Instance:
1 | public class MyClass : MonoBehaviour |
Categories: