由于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的对象
DontDestroyOnLoad(singleton);
}
}
return mInstance;
}
}
// 在A类里获得B类的实例,并根据该实例调用mIsLoading变量:
public class ClassB : Singleton<ClassB>
{
public bool mIsLoading = false;
}
public class ClassA
{
public static void Go()
{
Debug.Log(ClassB.GetInstance().mIsLoading);
}
}
当然,普通类也能写一个泛型单例类作为基类:
public class SimpleSingleton<T> where T : new() // new约束:T类里必须有无参构造函数
{
private static T mInstance;
public static T GetInstance()
{
if (null == mInstance)
{
mInstance = new T();
}
return mInstance;
}
}
法二:也可以不用泛型单例类,而是把单例代码放在需要做成单例的类里:
public class ClassB : MonoBehaviour
{
public bool mIsLoading = false;
private static ClassB mInstance;
public static ClassB GetInstance()
{
if (null == mInstance)
{
mInstance = FindObjectOfType(typeof(ClassB)) as ClassB;
if (null == mInstance)
{
GameObject singleton = new GameObject("(singleton)" + typeof(ClassB).ToString());
mInstance = singleton.AddComponent<ClassB>();
DontDestroyOnLoad(singleton);
}
}
return mInstance;
}
}
Unity截图: