//单纯得类单例
public class Singonton<T> where T:new()
{
public static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = new T();
}
return instance;
}
}
}
//继承Monobehavior的单例
using UnityEngine;
public abstract class Monosingleton<T> : MonoBehaviour
where T : MonoBehaviour,new()
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<T>() ?? new GameObject(typeof(T).ToString()).AddComponent<T>();//这里判断是否存在了这个类 存在了就不创建了
}
return instance;
}
}
}
单例就是全局唯一