可能习惯了写单例的朋友,或者常规的单例模式 会这样做
private static Single instance;
public static Single Instance(){
if (instance == null) {
instance = new Single();
}
return instance;
}
但是当你继承了MonoBehaviour 你就要小心了如果你这样写
public class Single : MonoBehaviour {
private static Single instance;
public static Single Instance(){
if (instance == null) {
instance = new Single();
}
return instance;
}
}
你会发现instance 永远为空的(即使走了这一步的instance = new Single();) 而且你回收到如下的警告
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
那么问题来了,第一个我们应该如何使用继承了MonoBehaviour 的单例,第二个为何new了 instance 却等于 null
第一个问题很简单,对MonoBehaviour 生命周期有了解的都知道应该如下做
public class Single : MonoBehaviour {
private static Single instance;
void Awake(){
instance = this;
}
public static Single Instance(){
return instance;
}
}
之所以放在Awake 是因为Awake是所有mono 调用start方法之前都会被调用的,这样可以避免某些调用的时候instance=null的情况
第二个查了一通untiy的文档得出的结论是
因为所有从MonoBehaviour继承过来的类,unity都会自动创建实例,并且调用被重载的方法,如我们经常用到的Awake, Start, Update等。
Unity does not allow you to instantiate anything inheriting from the MonoBehaviour class using the new
keyword. This seems a bit odd, until you consider what the MonoBehaviour class is.
MonoBehaviours are scripts that are attached to an object in the scene, and run in the scene as long as the object they are attached to is active. MonoBehaviours HAVE to be attached to something, or they won't be able to function properly. When you use new
, you are basically saying: "Please make one of these, store it somewhere, and give me a link to it". What you don't tell it is: "Also attach it to the place I'm calling it from". The reason this isn't done is because the concept of attaching things to objects is Unity specific, while the keyword new
is general to C#, and so has no concept of Unity constructions - it can't physically do this.
So how DO you specify what to attach it to? Well, Unity provides it's own method for this - namely GameObject.AddComponent()
. What this does is create a new script, of type T, and add it to the specified GameObject. So, if you have a GameObject called obj
and a script called MyScript
, you can dynamically add the script to your object by, instead of doing this:
MyScript script = new Script();
Which would, hypothetically, give you a script floating in space, not attached to anything, you can do this:
MyScript script = obj.AddComponent<MyScript>();
This will add a new MyScript
component to obj
, and then will set the variable script
to that component, so you can then access it.
Hopefully, you can see why this issue occurs, and what you can do to solve it, now.
总结一下就是: 这是unity的规则,如果你继承了 MonoBehaviour 请使用unity的规则来进行实例化这个类,至于想通过c# 的new 去实例化mono 的类是不被允许的。好吧~~规则如此我也无法解释了,就跟你无法解释很多的公式一个道理。也许有朋友理解比较深,可以教我一下,还有一点就是为何当我们去new了以后unity只是给了一个警告而已,却并没有给出error级别的log,这也变相的告诉我们开发者,要把所有的warn级别的也清掉了。否则可能导致意想不到的bug