using UnityEngine; namespace Core.Utilities { /// /// Singleton class,这种类型的Singleton赴作用,必须得在Unity中找一个Entity--Add Compoment,不然不会调用Awake. /// /// Type of the singleton public abstract class Singleton : MonoBehaviour where T : Singleton { /// /// The static reference to the instance /// public static T instance { get; protected set; } /// /// Gets whether an instance of this singleton exists /// public static bool instanceExists { get { return instance != null; } } /// /// Awake method to associate singleton with instance /// protected virtual void Awake() { if (instanceExists) { Destroy(gameObject); } else { instance = (T) this; } } /// /// OnDestroy method to clear singleton association /// protected virtual void OnDestroy() { if (instance == this) { instance = null; } } } }