using UnityEngine;
public class Singleton : MonoBehaviour
{
////饿汉式单例
////private static Singleton _instance = new();
//private static Singleton _instance ;
//public static Singleton Instance => _instance;
//private void Awake()=>_instance = this;
//懒汉式单例
private static Singleton _instance;
public static Singleton Instance
{
get
{
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}
}
//单例模板
public abstract class SingletonTemplate<T>:MonoBehaviour
where T : MonoBehaviour
{
private static T _instance;
public static T Instance => _instance;
private void Awake()=>_instance=this as T;
}