关闭

unity3d - new 不出的单例

2756人阅读 评论(0) 收藏 举报
分类:

可能习惯了写单例的朋友,或者常规的单例模式 会这样做


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:

  1. MyScript script = new Script();

Which would, hypothetically, give you a script floating in space, not attached to anything, you can do this:

  1. 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





2
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:285681次
    • 积分:3615
    • 等级:
    • 排名:第8997名
    • 原创:196篇
    • 转载:7篇
    • 译文:2篇
    • 评论:60条
    最新评论