diff options
Diffstat (limited to 'Client/Assembly-CSharp/DestroyableSingleton.cs')
-rw-r--r-- | Client/Assembly-CSharp/DestroyableSingleton.cs | 58 |
1 files changed, 58 insertions, 0 deletions
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<T> : MonoBehaviour where T : MonoBehaviour +{ + public static bool InstanceExists + { + get + { + return DestroyableSingleton<T>._instance; + } + } + + public static T Instance + { + get + { + if (!DestroyableSingleton<T>._instance) + { + DestroyableSingleton<T>._instance = UnityEngine.Object.FindObjectOfType<T>(); + if (!DestroyableSingleton<T>._instance) + { + DestroyableSingleton<T>._instance = new GameObject().AddComponent<T>(); + } + } + return DestroyableSingleton<T>._instance; + } + } + + private static T _instance; + + public bool DontDestroy; + + public virtual void Awake() + { + if (!DestroyableSingleton<T>._instance) + { + DestroyableSingleton<T>._instance = (this as T); + if (this.DontDestroy) + { + UnityEngine.Object.DontDestroyOnLoad(base.gameObject); + return; + } + } + else if (DestroyableSingleton<T>._instance != this) + { + UnityEngine.Object.Destroy(base.gameObject); + } + } + + public virtual void OnDestroy() + { + if (!this.DontDestroy) + { + DestroyableSingleton<T>._instance = default(T); + } + } +} |