Bootstrap

unity 单例类模板

    public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
    {

        protected static T instance;

        public static T Instance
        {
            get { return instance; }
        }


        public virtual void Awake()
        {
            if (Instance == null)
            {
                instance = this as T;
            }
            else
            {
                Destroy(gameObject);
            }
        }
    }

public class test : Singleton<test> //这样 test 就是一个单例类

{

}

test 为单例类

 

 

;