diff options
author | chai <215380520@qq.com> | 2023-05-12 10:32:11 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-05-12 10:32:11 +0800 |
commit | 2fc9585797067730f28b03b0727bf05f9deed091 (patch) | |
tree | 8807e37b85ba922045eaa17ac445dd0a1d2d730c /WorldlineKeepers/Assets/Scripts/Common/SingletonMB.cs | |
parent | 2a1cd4fda8a4a8e649910d16b4dfa1ce7ae63543 (diff) |
+ worldline keepers
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Common/SingletonMB.cs')
-rw-r--r-- | WorldlineKeepers/Assets/Scripts/Common/SingletonMB.cs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Common/SingletonMB.cs b/WorldlineKeepers/Assets/Scripts/Common/SingletonMB.cs new file mode 100644 index 0000000..5debe57 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Common/SingletonMB.cs @@ -0,0 +1,58 @@ +using Newtonsoft.Json.Utilities; +using UnityEngine; + +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 + { + Debug.LogError("m_Instance is already null!! type is " + typeof(T).ToString()); + } + } +}
\ No newline at end of file |