单例
public class Singleton<T> where T : new()
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = new T();
}
return instance;
}
}
}
Unity中的mono单例
public class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
private static T instance;
public static T Instance { get => instance; }
protected virtual void Awake()
{
if(instance != null)
Destroy(this);
instance = (T)this;
}
}