using UnityEngine;
public class SingleMono<T> : MonoBehaviour where T : MonoBehaviour
{
static protected T _instance;
static public T instance
{
get
{
Create();
return _instance;
}
}
static protected void Create()
{
if (_instance == null)
{
GameObject temp = new GameObject(typeof(T).Name);
_instance = temp.AddComponent<T>();
}
}
protected virtual void Awake()
{
T t = this.GetComponent<T>();
if (_instance == null)
{
_instance = t;
}
else
{
if (_instance != t)
{
this.GetComponent<T>();
Debug.LogErrorFormat("The [{0}] already exists", this.GetType().Name);
Destroy(this.gameObject);
}
}
}
}