Unity3D_c#脚本注意要点
-
1. Inherit from MonoBehaviour
继承自MonoBehaviourAll behaviour scripts must inherit from MonoBehaviour (directly or indirectly). This happens automatically in
Javascript, but must be explicitly explicitly inside C# or Boo scripts. If you create your script inside Unity through the Asset -> Create -> C Sharp/Boo Script menu, the created template will already contain the necessary definition.全部的行为脚本必须从MonoBehaviour继承(直接的或间接的).在JavaScript中这个是自己主动完毕的,可是在C#或Boo中,必须显示注明.假设你通过Asset -> Create -> C Sharp/Boo Script创建脚本,系统模版已经包括了必要的定义.
public class NewBehaviourScript : MonoBehaviour {...} // C#
class NewBehaviourScript (MonoBehaviour): ... # Boo
2. Use the Awake or Start function to do initialisation.
使用Awake或Start函数初始化.What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.
JavaScript中放在函数之外的代码,在C#或Boo中必须置于Awake或Start函数里.
The difference between Awake and Start is that Awake is run when a scene is loaded and Start is called just before the first call to an Update or a FixedUpdate function. All Awake functions are called before any Start functions are called.
Awake和Start的不同之处在于,Awake是在载入场景时执行,Start是在第一次调用Update或FixedUpdate函数之前被调用,Awake函数执行在全部Start函数之前.
3. The class name must match the file name.
类名字必须匹配文件名称.In Javascript, the class name is implicitly set to the file name of the script (minus the file extension). This must be done manually in C# and Boo.
JavaScript中类名被隐式的设置为脚本的文件名称(不包括文件扩展名).在C#和Boo中必须手工编写.
4. Coroutines have a different syntax in C#.
C#中协同程序有不同的句法规则Coroutines have to have a return type of IEnumerator and you yield using yield return ... ; instead of just yield ... ;.
Coroutines必须是IEnumerator返回类型,而且yield用yield return替代.
using System.Collections;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
// C# coroutine // C# 协同程序
IEnumerator SomeCoroutine () {
// Wait for one frame // 等一帧
yield return 0;// Wait for two seconds // 等两秒
yield return new WaitForSeconds (2);
}
}5. Don't use namespaces.
不要使用命名空间Unity doesn't support placing your scripts inside of a namespace at the moment. This requirement will be removed in a future version.
眼下Unity暂不支持命名空间.也许未来版本号会有.
6. Only member variables are serialized and are shown in the Inspector.
仅仅有序列化的成员变量才干显示在检视面板Private and protected member variables are shown only in Expert Mode. Properties are not serialized or shown in the inspector.
私有和保护变量仅仅能在专家模式中显示.属性不被序列化或显示在检视面板.
7. Avoid using the constructor.
避免使用构造函数Never initialize any values in the constructor. Instead use Awake or Start for this purpose. Unity automatically invokes the constructor even when in edit mode. This usually happens directly after compilation of a script, because the constructor needs to
be invoked in order to retrieve default values of a script. Not only will the constructor be called at unforeseen times, it might also be called for prefabs or inactive game objects.不要在构造函数中初始化不论什么变量.要用Awake或Start函数来实现.即便是在编辑模式,Unity仍会自己主动调用构造函数.这一般是在一个脚本编译之后,由于须要调用脚本的构造函数来取回脚本的默认值.我们无法估计何时调用构造函数,它也许会被预置体或未激活的游戏对象所调用.
In the case of eg. a singleton pattern using the constructor this can have severe consequences and lead to seemingly random null reference exceptions.
单一模式使用构造函数可能会导致严重后果,会带来类似随机的空參数异常.
So if you want to implement eg. a singleton pattern do not use the the constructor, instead use Awake . Actually there is no reason why you should ever have any code in a constructor for a class that inherits from MonoBehaviour .
因此,假设你想实现单一模式不要用构造函数,要用Awake函数.其实,你不是必需在继承自MonoBehaviour的类的构造函数中写不论什么代码.
Unity3D_c#脚本注意要点的更多相关文章
- 使用loadrunner录制脚本的思路和注意要点
基本思路如下图: 注意要点有如下几点: 1.性能测试往往需要准备大批量的数据,大批量数据的生成方法有很多种,常见的有: (1)编写SQL语句来插入数据 (2)使用DataFactory等专业的数据生成 ...
- JavaScript HTML5脚本编程——“历史状态管理”的注意要点
历史状态管理是现代Web应用开发中的一个难点.在现代Web应用中,用户的每次操作不一定会打开一个全新的页面,因此"后退"和"前进"按钮也就失去了作用,导致用户很 ...
- zabbix增加手机短信、邮件监控的注意要点,SSL邮件发送python脚本
1.短信接口文档: URL http://xxx.com/interfaces/sendMsg.htm Method POST Description 文字短信调用接口 Request Param L ...
- Apache执行Python脚本
由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...
- SQL Server镜像自动生成脚本
SQL Server镜像自动生成脚本 镜像的搭建非常繁琐,花了一点时间写了这个脚本,方便大家搭建镜像 执行完这个镜像脚本之后,最好在每台机器都绑定一下hosts文件,不然的话,镜像可能会不work 1 ...
- 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)
分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...
- 探真无阻塞加载javascript脚本技术,我们会发现很多意想不到的秘密
下面的图片是我使用firefox和chrome浏览百度首页时候记录的http请求 下面是firefox: 下面是chrome: 在浏览百度首页前我都将浏览器的缓存全部清理掉,让这个场景最接近第一次访问 ...
- 第一个shell脚本
打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...
- Java 8 的 Nashorn 脚本引擎教程
本文为了解所有关于 Nashorn JavaScript 引擎易于理解的代码例子. Nashorn JavaScript 引擎是Java SE 8的一部分,它与其它像Google V8 (它是Goog ...
随机推荐
- H3C交换机DHCP Server配置的六个方面
H3C交换机DHCP Server配置的六个方面 在交换机上面配置DHCP内容是司空见惯的了.那么这里我们就讲解一下H3C交换机DHCP Server配置内容.之后的文章中,我们还对针对其他方面的配置 ...
- 关于ssh加密方式的理解
最近公司服务器被挖矿,所以更换了ssh的连接方式,从之前的密码登陆更换为密钥登陆方式,且禁止了密码登陆.所以在配置这个密钥的过程中,顺带了解了些ssh的原理和相关知识.通用的开源 1.ssh是什么,为 ...
- SwiftUI 官方教程(三)
3. 用 Stacks 组合 View 在上一节创建标题 view 后,我们来添加 text view,它用来显示地标的详细信息,比如公园的名称和所在的州. 在创建 SwiftUI view 时,我们 ...
- Centos7中 文件大小排序
centos7中根据文件大小排序以及jenkins配置每周删除一次jobs日志信息 https://blog.csdn.net/u013066244/article/details/70232050
- (转载) popupWindow 指定位置上的显示
popupWindow 指定位置上的显示 标签: androidpopupWindowpopupWindow具体位置放置 2014-07-09 16:23 1114人阅读 评论(0) 收藏 举报 分 ...
- axis2 1.7.1使用教程
写在前面 本文只说Axis2的用法. 1.下载与部署 需要下载两个文件: 下载地址:http://mirrors.cnnic.cn/apache/axis/axis2/java/core/1.7.1/ ...
- TOF相机基本知识
TOF是Time of flight的简写,直译为飞行时间的意思.所谓飞行时间法3D成像,是通过给目标连续发送光脉冲,然后利用传感器接收从物体返回的光,通过探测光脉冲的飞行时间来得到目标物的距离.TO ...
- **PCL:嵌入VTK/QT显示(Code^_^)
中国人真是太不知道分享了,看看这个老外的博客,启发性链接. http://www.pcl-users.org/ 1. 这个是可用的源代码: 原文:I saw a thread with links t ...
- jQuery 父级,祖先,兄弟,等选择性操作
jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(&q ...
- Laravel 查询某天数据 whereDate