diff options
Diffstat (limited to 'YesCommander/Assets/Scripts/Common/SingletonMB.cs')
-rw-r--r-- | YesCommander/Assets/Scripts/Common/SingletonMB.cs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/YesCommander/Assets/Scripts/Common/SingletonMB.cs b/YesCommander/Assets/Scripts/Common/SingletonMB.cs new file mode 100644 index 0000000..83575e7 --- /dev/null +++ b/YesCommander/Assets/Scripts/Common/SingletonMB.cs @@ -0,0 +1,62 @@ +using UnityEngine; +using YC; + +namespace YC +{ + + public abstract class SingletonMB<T> : MonoBehaviour where T : class + { + protected static T m_Instance; + + public static T Instance + { + get { return m_Instance; } + set + { + if (m_Instance != null) + { + throw new System.ApplicationException("An instance was created duplicate!"); + } + + m_Instance = value; + } + } + + protected virtual void Awake() + { + //if (null != m_Instance) + //{ + // LogHelper.LogError(StringUtil.Concat("Exception: Duplicated Instance!! type is ", typeof(T).ToString(), ", plz send this error msg to hanjun!")); + //} + + m_Instance = gameObject.GetComponent<T>(); + } + + /// <summary> + /// CN: 加这个函数是为了同一个GameObject挂了多个Manager类,再OnDestroy里设置自己的单例为null + /// </summary> + protected virtual void OnDestroy() + { + //m_Instance = null; + DoWhenOnDestroy(); + } + + protected virtual void DoWhenOnDestroy() + { + + } + + public void ReleaseInstance() + { + if (m_Instance != null) + { + UnityEngine.Object.Destroy(this.gameObject); + //m_Instance = null; + } + else + { + LogHelper.LogError("m_Instance is already null!! type is " + typeof(T).ToString()); + } + } + } +}
\ No newline at end of file |