ClockStone Audio Toolkit for Unity - Documentation
SingletonMonoBehaviourT Class |
Provides singleton-like access to a unique instance of a MonoBehaviour.
Inheritance Hierarchy
SystemObject Object
Component
Behaviour
MonoBehaviour
(Default Namespace)SingletonMonoBehaviourT
(Default Namespace)AudioController
Component
Behaviour
MonoBehaviour
(Default Namespace)SingletonMonoBehaviourT
(Default Namespace)AudioController
Namespace: (Default Namespace)
Assembly: AudioToolkit (in AudioToolkit.dll) Version: 8.2.0.0 (8.2.0.0)
Syntax
C#
public abstract class SingletonMonoBehaviour<T> : MonoBehaviour where T : MonoBehaviour
Type Parameters
- T
- Your singleton MonoBehaviour
The SingletonMonoBehaviourT type exposes the following members.
Properties
Name | Description | |
---|---|---|
Instance |
Gets the singleton instance.
| |
isSingletonObject |
must return true if this instance of the object is the singleton. Can be used to allow multiple objects of this type
that are "add-ons" to the singleton.
|
Methods
Name | Description | |
---|---|---|
ActivateSingletonInstance |
Activates the singleton instance.
| |
DoesInstanceExist |
Checks if an instance of this MonoBehaviour exists.
| |
SetSingletonAutoCreate |
Sets the object to be instantiated automatically if no instance of the singleton is found.
| |
SetSingletonType |
Only required for Flash builds. If this function is not called by the class deriving from
SingletonMonoBehaviour in the constructor the singleton can not be found by GetSingleton(...)
|
Remarks
Makes sure that an instance is available from other Awake() calls even before the singleton's Awake()
was called. ( Requires AwakeSingleton() !)
Examples
Derive your own class from SingletonMonoBehaviour.
public class MyScriptClass : SingletonMonoBehaviour<MyScriptClass> { public MyScriptClass() { MyScriptClass.SetSingletonType( typeof( MyScriptClass ) ); // workaround for Flash } public void MyFunction() { } protected override void Awake() { base.Awake(); } void AwakeSingleton() { // all initialisation code here. Will get called from Awake() by singleton. // Can get called before Awake() if an instance is accessed in an Awake() function which // was called earlier } }
access the instance by writing
MyScriptClass.Instance.MyFunction();
See Also