public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance;
public static T Instance
{
get
{
if (instance == null)
{
GameObject go = new GameObject(typeof(T).ToString());
go.AddComponent < T > ();
instance = FindObjectOfType(typeof(T)) as T;
}
return instance;
}
}
}
public class SingleManager
{
private static Dictionary<Type, object> dict = new Dictionary<Type, object>();
public static T GetInstance<T>()
{
Type t = typeof(T);
if (dict.ContainsKey(t))
{
return (T) dict[t];
}
else
{
if (!t.GetType().IsAssignableFrom(typeof(UnityEngine.MonoBehaviour)))
{
T temp = Activator.CreateInstance<T>();
dict.Add(temp.GetType(), temp);
return temp;
}
else
{
object temp2 = UnityEngine.GameObject.FindObjectOfType(typeof(T));
if (temp2 != null)
{
return (T) temp2;
}
}
}
return default(T);
}
public static void AddObject(object t)
{
if (!dict.ContainsKey(t.GetType()))
{
dict.Add(t.GetType(), t);
}
}
}