using System.Collections; using System.Collections.Generic; using UnityEngine; namespace TFramework { /// /// Mono单例模式基类 /// /// [DisallowMultipleComponent] [DefaultExecutionOrder(-10000)] public class SingletonBehaviourBase : MonoBehaviour where T : MonoBehaviour { public bool doNotDestroy; private static T _instance; /// /// 当前实例 /// public static T Instance => _instance; protected virtual void Awake() { if (_instance == null) _instance = GetComponent(); else throw new UnityException($"单例类【{typeof(T).FullName}】发现两个或以上实例,这是不被允许的!"); if (doNotDestroy&&_instance!=null) { DontDestroyOnLoad(gameObject); } } protected virtual void OnDestroy() => _instance = null; } }