using UnityEngine; namespace WZ { public abstract class NormalSingleton where T : new() { private static T mInstance; public static T Instance { get { if (mInstance == null) { mInstance = new T(); } return mInstance; } } } /// /// 动态(Dynamic) /// public abstract class D_MonoSingleton : MonoBehaviour where T : D_MonoSingleton { private static T mInstance = null; public static T Instance { get { if (mInstance == null) { GameObject tFramewokGo = GameObject.Find("TKFramework"); if (tFramewokGo == null) { tFramewokGo = new GameObject("TKFramework"); DontDestroyOnLoad(tFramewokGo); } GameObject tGo = new GameObject(); tGo.transform.SetParent(tFramewokGo.transform); tGo.name = typeof(T).ToString(); tGo.transform.localPosition = Vector3.zero; tGo.transform.localEulerAngles = Vector3.zero; tGo.transform.localScale = Vector3.one; mInstance = tGo.AddComponent(); } return mInstance; } } protected Transform mTrans; private void Awake() { mTrans = transform; Initialized(); } protected virtual void Initialized() { } private void OnDestroy() { mInstance = null; Dispose(); } protected virtual void Dispose() { } } /// /// 静态(static) /// public abstract class S_MonoSingleton : MonoBehaviour where T : S_MonoSingleton { private static T mInstance = null; public static T Instance { get { return mInstance; } } private void Awake() { if (mInstance != null && mInstance != (T)this) { Destroy(gameObject); return; } mInstance = (T)this; Initialize(); } protected virtual void Initialize() { } private void OnDestroy() { mInstance = null; Dispose(); } protected virtual void Dispose() { } } /// /// 静态常驻 /// public abstract class O_MonoSingleton : MonoBehaviour where T : O_MonoSingleton { private static T mInstance = null; public static T Instance { get { return mInstance; } } private void Awake() { if (mInstance != null && mInstance != (T)this) { Destroy(gameObject); return; } mInstance = (T)this; DontDestroyOnLoad(gameObject); Initialize(); } protected virtual void Initialize() { } private void OnDestroy() { mInstance = null; Dispose(); } protected virtual void Dispose() { } } }