public abstract class Singleton<T> : MonoBehaviour where T : Singleton<T>
{
protected static T s_instance = null;
public static T Instance
{
get
{
return GetInstance();
}
}
public virtual void Awake()
{
s_instance = this as T;
}
public static T GetInstance()
{
if (s_instance == null)
{
var name = typeof(T).Name;
var obj = GameObject.Find(name);
if (obj == null)
{
obj = new GameObject(name);
DontDestroyOnLoad(obj);
}
s_instance = obj.GetComponent<T>();
if (s_instance == null)
{
s_instance = obj.AddComponent<T>();
}
}
return s_instance;
}
}
public abstract class MgrBase<T> : Singleton<T> where T : Singleton<T>
{
}
public class SomeFunctionMgr : MgrBase<SomeFunctionMgr>
{
}