UnityUtilities
UnityUtilities.SingletonMonoBehaviour< TSubclass > Class Template Reference

A MonoBehaviour that should only exist once and provides easy access to its instance. However, it isn't automatically created and still needs to be attached to a GameObject in the scene. More...

Inheritance diagram for UnityUtilities.SingletonMonoBehaviour< TSubclass >:

Protected Member Functions

virtual void OnDestroy ()
 If this is the instance and it was destroyed, remove the instance registration. More...
 

Properties

static TSubclass Instance [get]
 Returns the sole instance of this class in the current scene. If the instance is not yet found cached, it searches for it and then caches it. More...
 
static TSubclass InstanceOptional [get]
 Returns the sole instance of this class in the current scene. If the instance is not already cached, it searches for it and then caches it. Note that if the instance is not found at all, InstanceOptional will try to find it on every call. For that reason, it is better to only call this once per script in Awake() or Start() and cache the result. More...
 

Detailed Description

A MonoBehaviour that should only exist once and provides easy access to its instance. However, it isn't automatically created and still needs to be attached to a GameObject in the scene.

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

Member Function Documentation

§ OnDestroy()

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

If this is the instance and it was destroyed, remove the instance registration.

Note: This also means that you need to use

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

in subclasses.

Property Documentation

§ Instance

TSubclass UnityUtilities.SingletonMonoBehaviour< TSubclass >.Instance
staticget

Returns the sole instance of this class in the current scene. If the instance is not yet found cached, it searches for it and then caches it.

§ InstanceOptional

TSubclass UnityUtilities.SingletonMonoBehaviour< TSubclass >.InstanceOptional
staticget

Returns the sole instance of this class in the current scene. If the instance is not already cached, it searches for it and then caches it. Note that if the instance is not found at all, InstanceOptional will try to find it on every call. For that reason, it is better to only call this once per script in Awake() or Start() and cache the result.


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