using UnityEngine;
using System.Collections;
using System; // 使用System头文件才能调用销毁方法
public class Singleton<T> : IDisposable where T :new()
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
instance = new T();
}
return instance;
}
}
public virtual void Dispose() //销毁方法
{
}
}
使用这个方法的好处 :使用了泛型T实现了逻辑的复用,避免了重复撰写单例逻辑
注:where关键字是约束所使用的泛型,该泛型必须是where后面的类;必须有无参构造,为了能够正确的初始化对象