using UnityEngine; namespace CleverCrow.Fluid.Utilities { // @SRC http://wiki.unity3d.com/index.php/Singleton public abstract class Singleton : MonoBehaviour where T : MonoBehaviour { private static T _instance; public static bool IsInstance => _instance != null; private static string SingletonName => typeof(T).Name; public static T Instance { get { if (_instance != null) { return _instance; } var instances = FindObjectsOfType(typeof(T)); if (instances.Length >= 1) { if (Application.isPlaying) { Debug.AssertFormat( instances.Length == 1, "{1} {0} singletons detected. There should only ever be one present", SingletonName, instances.Length); } _instance = (T)instances[0]; return _instance; } var singleton = new GameObject(SingletonName); _instance = singleton.AddComponent(); // Only add DontDestroyOnLoad if the user fails to manually implement the component in a scene // Thereby giving the user more control over the singleton lifecycle if (Application.isPlaying) { DontDestroyOnLoad(singleton); } return _instance; } } protected virtual void OnDestroy () { if (_instance == this) { _instance = null; } } /// /// Test friendly method to prevent persisting singletons with editor tests /// public static void ClearSingleton () { if (Application.isPlaying || _instance == null) { return; } DestroyImmediate(_instance.gameObject); _instance = null; } } }