using System.Diagnostics; namespace GameBase { /// /// 全局对象必须继承于此。 /// /// 子类类型。 public abstract class Singleton : ISingleton where T : Singleton, new() { protected static T _instance = default(T); public static T Instance { get { if (null == _instance) { _instance = new T(); _instance.Init(); SingletonSystem.Retain(_instance); } return _instance; } } public static bool IsValid => _instance != null; protected Singleton() { #if UNITY_EDITOR string st = new StackTrace().ToString(); // using const string to compare simply if (!st.Contains("GameBase.Singleton`1[T].get_Instance")) { UnityEngine.Debug.LogError($"请必须通过Instance方法来实例化{typeof(T).FullName}类"); } #endif } protected virtual void Init() { } public virtual void Active() { } public virtual void Release() { if (_instance != null) { SingletonSystem.Release(_instance); _instance = null; } } } }