由于Unity是组件化编程,除了对象以外一切都是组件(Component),所有继承自MonoBehaviour的脚本都要绑定到游戏对象(GameObject)上才能运行。如果某类继承了MonoBehaviour,就得按Unity的规则(也就是用AddComponent)来实例化该类,至于想通过C#的new去实例化mono的类是不被允许的。因此,如果写的脚本是独立于游戏对象之外的话,就不要让其继承自MonoBehaviour。
法一:继承自MonoBehaviour类能写一个泛型单例类作为基类:
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T mInstance;
public static T GetInstance()
{
if (null == mInstance)
{
mInstance = FindObjectOfType(typeof(T)) as T; // 获得场景中有T组件的the first active loaded gameobject of this type
//Debug.Log(FindObjectsOfType(typeof(T)).Length);
if (null == mInstance)
{
GameObject singleton = new GameObject("(singleton)" + typeof(T).ToString());
mInstance = singleton.AddComponent<T>(); // 实例化T的对象