单例模式
单例模式的通用写法:一个public static,一个private static
不继承 MonoBehaviour
public class BaseManager<T> where T : class,new()
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
instance = new T();
return instance;
}
}
}
继承 MonoBehaviour
public class SingleMono<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T Instance
{
get
{
if(instance == null)
{
GameObject obj = new GameObject();
obj.name = typeof(T).ToString();
instance = obj.AddComponent<T>();
DontDestroyOnLoad(obj);
}
return instance;
}
}
}