UnityUtilities
UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass > Class Template Reference

Any class that extends from this one will persist through scenes (via Object.DontDestroyOnLoad) and can be handily accessed via the static Instance property. More...

Inheritance diagram for UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass >:

Protected Member Functions

virtual void OnDestroy ()
 If this is the persistent instance and it was destroyed (manually), remove the instance registration. More...
 
virtual void Start ()
 Disable skipOnLevelWasLoaded. More...
 
virtual void OnPersistentSingletonAwake ()
 This method is called when the Awake() method of the first instance of the persistent singleton is done. This is not called if this is a second instance (which is destroyed automatically immediately). More...
 
virtual void OnPersistentSingletonDestroyed ()
 This method is called when the registered instance of the persistent singleton is either destroyed manually by calling Destroy() or the application is closed. This is not called if this is a second instance (which is destroyed automatically immediately). More...
 
virtual void OnSceneSwitched ()
 This method is called after switching to a new scene. More...
 
virtual void OnAwakeOrSwitch ()
 This method is called immediately after OnPersistentSingletonAwake or OnSceneSwitched. More...
 

Properties

static TSubclass Instance [get]
 Returns the singleton instance. More...
 
bool InstanceExists [get]
 Returns true if a singleton instance is registered. More...
 

Detailed Description

Any class that extends from this one will persist through scenes (via Object.DontDestroyOnLoad) and can be handily accessed via the static Instance property.

If a new scene is loaded with a new instance of this class while another persistent instance already exists, the new instance in the scene is destroyed. This allows you to make this a prefab and have it in all scenes that you are working on.

Template Parameters
TSubclassThe subclass that extends from this class.
Type Constraints
TSubclass :PersistentSingletonMonoBehaviour<TSubclass> 

Member Function Documentation

§ OnAwakeOrSwitch()

virtual void UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass >.OnAwakeOrSwitch ( )
protectedvirtual

This method is called immediately after OnPersistentSingletonAwake or OnSceneSwitched.

§ OnDestroy()

virtual void UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass >.OnDestroy ( )
protectedvirtual

If this is the persistent instance and it was destroyed (manually), remove the instance registration.

Note: This also means that you need to use

protected override void OnDestroy()
{
    base.OnDestroy();
    // [Your code]
}

in subclasses.

§ OnPersistentSingletonAwake()

virtual void UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass >.OnPersistentSingletonAwake ( )
protectedvirtual

This method is called when the Awake() method of the first instance of the persistent singleton is done. This is not called if this is a second instance (which is destroyed automatically immediately).

§ OnPersistentSingletonDestroyed()

virtual void UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass >.OnPersistentSingletonDestroyed ( )
protectedvirtual

This method is called when the registered instance of the persistent singleton is either destroyed manually by calling Destroy() or the application is closed. This is not called if this is a second instance (which is destroyed automatically immediately).

§ OnSceneSwitched()

virtual void UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass >.OnSceneSwitched ( )
protectedvirtual

This method is called after switching to a new scene.

§ Start()

virtual void UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass >.Start ( )
protectedvirtual

Disable skipOnLevelWasLoaded.

Property Documentation

§ Instance

TSubclass UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass >.Instance
staticget

Returns the singleton instance.

§ InstanceExists

bool UnityUtilities.PersistentSingletonMonoBehaviour< TSubclass >.InstanceExists
getprotected

Returns true if a singleton instance is registered.


The documentation for this class was generated from the following file: