单例类
public abstract class Singleton<T> where T:class, new()
{
private static T mInstance;
public static T GetInstance()
{
if(mInstance == null)
{
mInstance = new T();
}
return mInstance;
}
}
继承MonoBehaviour的单例
public class SingletonMono<T> : MonoBehaviour where T: MonoBehaviour
{
private static T mInstance;
public bool isDoneDestoryOnLoad = false;
public static T GetMonoInstance
{
get
{
if(mInstance == null)
{
mInstance = (T)FindObjectOfType(typeof(T));
#if UNITY_EDITOR
if(FindObjectsOfType(typeof(T)).Length > 1)
{
Debug.LogWarning("Only have one Singleton, but you have more onefor <T>.");
}
#endif
return mInstance;
}
return mInstance;
}
}