namespace SRF.Components { using System; using System.Diagnostics; using UnityEngine; using Debug = UnityEngine.Debug; /// /// Inherit from this component to easily create a singleton gameobject. /// /// public abstract class SRSingleton : SRMonoBehaviour where T : SRSingleton { private static T _instance; /// /// Get the instance of this singleton. /// ().Length == 2) { Destroy(gameObject); } else { Destroy(this); } return; } _instance = (T) this; } // If no other monobehaviour request the instance in an awake function // executing before this one, no need to search the object. protected virtual void Awake() { Register(); } protected virtual void OnEnable() { // In case of code-reload, this should restore the single instance if (_instance == null) { Register(); } } #if UNITY_EDITOR private void OnApplicationQuit() { // Null any references when exiting play mode. _instance = null; } #endif } }