|
- using System;
- using UnityEngine;
-
- namespace SUISS.Core
- {
- public abstract class StaticMonoBehaviour<T> : MonoBehaviour where T : StaticMonoBehaviour<T>
- {
- public static bool IsAvailable
- {
- get
- {
- return !object.ReferenceEquals(StaticMonoBehaviour<T>._instance, null);
- }
- }
-
- public static T Instance
- {
- get
- {
- if (object.ReferenceEquals(StaticMonoBehaviour<T>._instance, null))
- {
- UnityEngine.Debug.LogWarning("Could not find an instance of " + typeof(T).Name + ". Make sure it is in the scene and to give it time for its Awake() to be called.");
- return (T)((object)null);
- }
- return StaticMonoBehaviour<T>._instance;
- }
- }
-
- protected virtual void Awake()
- {
- if (object.ReferenceEquals(StaticMonoBehaviour<T>._instance, null))
- {
- StaticMonoBehaviour<T>._instance = (T)((object)this);
- }
- else
- {
- UnityEngine.Debug.LogError("Found multiple instances of " + typeof(T).Name + " in the scene.");
- }
- }
-
- protected virtual void OnDestroy()
- {
- if (object.ReferenceEquals(StaticMonoBehaviour<T>._instance, this))
- {
- StaticMonoBehaviour<T>._instance = (T)((object)null);
- }
- }
-
- private static T _instance;
- }
- }
|