From e9ea621b93fbb58d9edfca8375918791637bbd52 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 30 Dec 2020 20:59:04 +0800 Subject: +init --- Client/Assembly-CSharp/DestroyableSingleton.cs | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Client/Assembly-CSharp/DestroyableSingleton.cs (limited to 'Client/Assembly-CSharp/DestroyableSingleton.cs') diff --git a/Client/Assembly-CSharp/DestroyableSingleton.cs b/Client/Assembly-CSharp/DestroyableSingleton.cs new file mode 100644 index 0000000..bcd8319 --- /dev/null +++ b/Client/Assembly-CSharp/DestroyableSingleton.cs @@ -0,0 +1,58 @@ +using System; +using UnityEngine; + +public class DestroyableSingleton : MonoBehaviour where T : MonoBehaviour +{ + public static bool InstanceExists + { + get + { + return DestroyableSingleton._instance; + } + } + + public static T Instance + { + get + { + if (!DestroyableSingleton._instance) + { + DestroyableSingleton._instance = UnityEngine.Object.FindObjectOfType(); + if (!DestroyableSingleton._instance) + { + DestroyableSingleton._instance = new GameObject().AddComponent(); + } + } + return DestroyableSingleton._instance; + } + } + + private static T _instance; + + public bool DontDestroy; + + public virtual void Awake() + { + if (!DestroyableSingleton._instance) + { + DestroyableSingleton._instance = (this as T); + if (this.DontDestroy) + { + UnityEngine.Object.DontDestroyOnLoad(base.gameObject); + return; + } + } + else if (DestroyableSingleton._instance != this) + { + UnityEngine.Object.Destroy(base.gameObject); + } + } + + public virtual void OnDestroy() + { + if (!this.DontDestroy) + { + DestroyableSingleton._instance = default(T); + } + } +} -- cgit v1.1-26-g67d0