unity3d - new 不出的单例
可能习惯了写单例的朋友,或者常规的单例模式 会这样做
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
unity3d - new 不出的单例的更多相关文章
- Swift百万线程攻破单例(Singleton)模式
一.不安全的单例实现 在上一篇文章我们给出了单例的设计模式,直接给出了线程安全的实现方法.单例的实现有多种方法,如下面: class SwiftSingleton { class var shared ...
- 【Spring源码分析】非懒加载的单例Bean初始化过程(上篇)
代码入口 上文[Spring源码分析]Bean加载流程概览,比较详细地分析了Spring上下文加载的代码入口,并且在AbstractApplicationContext的refresh方法中,点出了f ...
- Spring源码分析:非懒加载的单例Bean初始化过程(上)
上文[Spring源码分析]Bean加载流程概览,比较详细地分析了Spring上下文加载的代码入口,并且在AbstractApplicationContext的refresh方法中,点出了finish ...
- Unity3D中可中途释放的单例
Unity3D中可中途释放的单例 使用静态类,静态变量的坏处是从程序加载后就一直占用内存,想要释放比较麻烦,可是之前使用的单例,没有提供释放的方法,那是不是也同静态的一样直到程序结束菜释放?那单例的好 ...
- unity3d中设计模式的学习<一>:泛型单例
单例是游戏开发中比较常见的设计模式,虽然针对的功能不同,但是有一些功能还是共有的,代码也不少,如果能放在一个基类里面是最好不过了,但是单例里需要有个instance功能来返回当前对象,所以这个功能必须 ...
- 【转】Unity3d的单例及场景间的数据传递
http://blog.csdn.net/zy19940906/article/details/47724387 单例是场景间切换时传递数据的最常见的方式之一,在unity中,很多方法被封装,有时候 ...
- [Android面试题-7] 写出一个Java的Singleton类(即单例类)
1.首先明确单例的概念和特点: a>单例类只能有一个实例 b>单例类必须自己创建一个自己的唯一实例 c>单例类必须为其他所有对象提供这个实例 2.单例具有几种模式,最简单的两种分别是 ...
- 如何写出面试官欣赏的Java单例
单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例. 今天我们不谈单例模式的用途,只说一说如果在面试的时候面试官让你敲一段代码 ...
- Unity Singleton 单例类(Unity3D开发之二十)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/47335197 ...
随机推荐
- Python学习笔记——文件操作
python中,一切皆对象. 一.文件操作流程 (1)打开文件,得到一个文件句柄(对象),赋给一个对象: (2)通过文件句柄对文件进行操作: (3)关闭文件. 文件对象f通过open()函数来创建 ...
- Android Studio debug使用release的签名
当我们在做微信微博sdk分享的时候调试非常麻烦,因为要使用对应的签名版本才能调用sdk成功. 当我们使用AndroidStudio的Gradle之后会很简单的解决这个问题. 1.我们把签名文件放到工程 ...
- java初探/java读取文件
import java.io.*; import java.util.Arrays; public class WriteText { public static void main(String[] ...
- java 集合的使用 (一)
1.使用整型列表 List<int> lstInt=new List<int>(); 结果不对,报的错误是:Syntax error, insert "Dimensi ...
- [linux] FastDFS访问文件,400 Bad Request
linux 安装nginx,FastDFS后,启动访问指定文件出错, 文件名称格式化错误. 解决办法: vi /etc/fdfs/mod_fastdfs.conf 将 url_have_group_n ...
- 【SVN】自动备份SVN仓库
仓库的位置为:C:\xxx\SVNRepo\ MyCommonUtils MyStudyProject SVN仓库备份.bat '参考连接:http://www.uml.org.cn/pzgl/201 ...
- 【uTenux实验】邮箱
邮箱是一个通过在系统(共享)内存空间传递消息来实现同步和通信的对象.uTenux中每个邮箱都包含一个用来发送消息的消息队列和一个用于等待接收消息的任务队列,其使用邮箱功能的消息内容放置在发送方和接收方 ...
- java程序打包成jar文件,使用到第三方jar包
1.右击工程选择Export—>选择JAR file—>选择NEXT,如下图所示 2.选择需要打包的工程,并且选择存放目录,我这放在 E:\jartest 目录下,然后点击NEXT,如下图 ...
- JS中数组Array的用法
js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了. var arr = new Array(); // 初始化数组arr[0] = "aaa";arr[1] = ...
- Embed dll Files Within an exe (C# WinForms)—Winform 集成零散dll进exe的方法
A while back I was working on a small C# WinForms application in Visual Studio 2008. For the sake of ...