// Copyright 2021 by Hextant Studios. https://HextantStudios.com // Licensed under CC BY 4.0. http://creativecommons.org/licenses/by/4.0/ using UnityEngine; // A simple MonoBehaviour-based singleton for use at runtime. // Note: This implementation does not support the "Recompile and Continue Playing" // editor preference! public class SimpleMonoBehaviourSingleton : MonoBehaviour where T : SimpleMonoBehaviourSingleton { // The singleton instance. public static T instance => _instance; static T _instance; // Called when the instance is created. protected virtual void Awake() { // Verify there is not more than one instance and assign _instance. Debug.Assert( _instance == null, "More than one singleton instance instantiated!", this ); _instance = ( T )this; } // Clear the instance field when destroyed. protected virtual void OnDestroy() => _instance = null; }