Class SingletonBehavior<TDerived>
Base class that applies the singleton design pattern to a UnityEngine.MonoBehaviour, providing a shared instance of the given type
Inheritance
System.Object
UnityEngine.Object
UnityEngine.Component
UnityEngine.Behaviour
UnityEngine.MonoBehaviour
SingletonBehavior<TDerived>
Namespace: Atlas
Assembly: Atlas.Runtime.dll
Syntax
public abstract class SingletonBehavior<TDerived> : MonoBehaviour where TDerived : MonoBehaviour
Type Parameters
Name | Description |
---|---|
TDerived | Type of the derived class |
Examples
namespace Atlas.Examples
{
public sealed class Example_SingletonBehavior
{
// declare singleton class
public sealed class AudioSystem : SingletonBehavior<AudioSystem>
{
public void PlayAudioClip( int id )
{
// Audio playback code...
}
}
// called when a goal is scored
public void OnGoalScored()
{
// play audio using shared AudioSystem instance
AudioSystem.Instance.PlayAudioClip( c_goalAudioID );
}
private const int c_goalAudioID = 5;
}
}
Properties
| Improve this Doc View SourceHasInstance
Whether or not an instance exists. Since destruction order isn't guaranteed in Unity, it's recommended to reference this property when accessing singletons in shutdown methods such as OnDestroy
Declaration
public static bool HasInstance { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Instance
The instance of the class. If an instance does not yet exist, an attempt will be made to find one using UnityEngine.Object.FindObjectOfType``1. If an instance is still not found, an UnityEngine.GameObject will be instantiated with the desired class instance attached.
Declaration
public static TDerived Instance { get; }
Property Value
Type | Description |
---|---|
TDerived |
Methods
| Improve this Doc View SourceAwake()
Declaration
protected virtual void Awake()
OnDestroy()
Declaration
protected virtual void OnDestroy()