SINGLETON
Last updated
Last updated
SINGLETON --> a software engineering concept where a program has global access to a class, that class can only exist ONCE
The recommended classes to use the singleton design pattern on is with a programs manager classes
--> Game Manager Class --> Player Manager Class --> UI Manager Class --> Item Manager --> Spawn Manager
Manager classes are the optimal classes to implement the singleton design pattern because they should be the only class of its type within the program.
LAZY INSTANTIATION --> creates an instance of the singleton class IF it is null
Lazy instantiation is one of the benefits of working with the singleton pattern as it allows a program a failsafe if an instance of a singleton class is null when called. --> But, the best practice is still to make sure the programs singleton classes are not NULL
The downfall to Lazy Instantiation is that if there were values that needed to be assigned within the inspector, those values will not be attached because the class is instantiated on the fly with it's default values.
MonoSingleton --> the ability to turn any class easily into a singleton
To create a MonoSingleton you need to create a new generic abstract class called MonoSingleton --> will be used as a generic template for all Singletons moving forward in the program
public abstract class
--> makes MonoSingleton act as a template
Cannot attach class to a gameobject
other classes can inherit & gain all the properties
MonoSingleton<T> : MonoBehaviour
--> Defining a generic type of this MonoSingleton
Inherits from MonoBehaviour
where T: MonoSingleton<T>
--> WHERE keyword is a constraint on type parameters
without constraints, the type argument can be any type