using UnityEngine; /// /// 动态(Dynamic) /// public abstract class D_MonoSingleton : MonoBehaviour where T : D_MonoSingleton { private static T _instance = null; public static T Instance { get { if (_instance == null) { GameObject go = new GameObject(); DontDestroyOnLoad(go); go.name = "MonoSingleton:" + typeof(T).ToString(); go.transform.localPosition = Vector3.zero; go.transform.localEulerAngles = Vector3.zero; go.transform.localScale = Vector3.one; _instance = go.AddComponent(); } return _instance; } } private void Awake() { Initialize(); } protected virtual void Initialize() { } private void OnDestroy() { _instance = null; } } /// /// 静态(static) /// public abstract class S_MonoSingleton : MonoBehaviour where T : S_MonoSingleton { private static T _instance = null; public static T Instance { get { return _instance; } } private void Awake() { if (_instance != null && _instance != (T)this) { Destroy (gameObject); return; } _instance = (T)this; Initialize (); } protected virtual void Initialize() { } private void OnDestroy() { _instance = null; } }