Base class to implement the singleton pattern for UnityEngine.MonoBehaviour
.
More...
Inherits MonoBehaviour.
Static Public Attributes | |
static T | instance => Singleton<T>.instance |
Get the singleton instance. More... | |
Protected Member Functions | |
BaseSingleton () | |
Constructor to check inheritance, since we can't constraint subclass. More... | |
virtual void | Awake () |
Awake, check and handle duplicated instances. More... | |
Static Protected Attributes | |
static bool | autoDestroy |
Flag if to destroy excess instance automatically when it Awake() . More... | |
Base class to implement the singleton pattern for UnityEngine.MonoBehaviour
.
This works with Singleton
to minimize coding, if you don't need custom inheritance. Derive from this to make a UnityEngine.MonoBehaviour
singleton. Only allow the class of current singleton to derive, otherwise throw an exception if in the editor. This applies the singleton's root Object.DontDestroyOnLoad()
and handle duplicates when Awake()
.
Example to implement singleton, just derive from this:
Example to access from another class:
T | The component type. |
T | : | BaseSingleton<T> |
|
protected |
Constructor to check inheritance, since we can't constraint subclass.
|
protectedvirtual |
Awake, check and handle duplicated instances.
Apply Object.DontDestroyOnLoad()
to the root if this's the singleton. Otherwise check if autoDestroy
to suicide or log error. Call base.Awake()
if you implement Awake()
in the subclass.
Reimplemented in FramerateCounter.
|
staticprotected |
Flag if to destroy excess instance automatically when it Awake()
.
|
static |
Get the singleton instance.