Singleton Design Pattern.
Objective: Singleton Design Pattern.
Singleton Design pattern is the pattern which allow instance to appear only once and allow to be accessed through global level.
We start by define private static variable of class type, we rename it using camel case.
The next step is to define public static property of the same type, but this time we use Pascal Case naming conversion. Then we check if the camel case instance is null.
To allow other class to access this instance we need to return the lower case instance after if condition.
Now we need to assign the Instance before the game start, we do this in Awake method. By setting lower case instance to this script, this means we are pointing this variable to this class.
To access this class to other class.
We access this class using class level, meaning we use class name then go inner and call Camel Case Instance.
Singleton Lazy Instantiation
We do this to make sure we have Game Object then add our type(“GameManager script”) to this game object. We do this by create new game object instead of prompt error if the game object is null in the scene.
Note: I will delete Game Manager game object to demonstrate this.
Corn of lazy Instantiation.
It will throw error if you have field in game manager, so make sure to add the field if you will use this.
It is not recommended to this way when use singleton, cos if the object have 1000 field and bad luck it was deleted it will take you hour to refill .
Solution for second corn point.
To use this you need to add logic to refill the field in dynamic way.
MonoSingleton
Is the way allow us to create a template for singleton so every script want to use singleton can just inherit this and avoid duplicate of code.
We start by creating abstract class, I will rename it Monosingletone.
Then we add generic type after class name of type capital T which is generic type, this allow us to pass any type in T bracket if we Inherit this class.
Note: Since this class is abstract type it will force us to pass a type when we inherit this class otherwise you will get error if you inherit Monosingleton without T bracket. Please read my article about abstract if you dont know what is abstract class.
Now we need to define how T Inherit Monosingleton, We do this after Monobehaviour by using where Keyword and add T then colon followed by Monosingleton Generit type this mean T will Inherit Monosingleton generic type T.
Note: This mean T type will Inherit MonosingleTon, then generic type of Monosingleton will be a Type.
Creating Singleton in Monosingleton Script.
To create singleton in a Monosingleton a type need to be T since is the type need to have singleton and its the one will be attached to game object, remember Monosingleton is just template is not going to be attached to any object.
Assign instance to Monosingleton in Awake
To Assign the instance in Monosingleton we need to cast by T after equal sing to allow a class Inherit Monosingleton is the instance used as reference type.
Inheriting Monosingleton .
The script must Inherit one base class, this means we need to delete monobehavior from this derived class since Monobeheviour since already inherited from Monosingletone.
Here is small explanation from Microsoft documentation,if my is confusing.
When Inherit Monosingleton must add T bracket and pass Derived class as Type since we define this as signature to any class which will inherit Monosingleton.
Calling Game Manager in player .
See you into the next one.