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 ...
随机推荐
- 开源DDos 机器学习思路求解的一些源码——TODO 待分析
一些源码:https://github.com/elbaulp/MafDet System that aims to detect and mitigate DDoS attacks using Ma ...
- HDU 2520 我是菜鸟我怕谁
2019-05-27 17:52:01 加油!!! 看题时候就要仔细,最后容易忘记%10000 #include <bits/stdc++.h> using namespace std; ...
- JavaScript之BOM和DOM
前戏 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些简单的语法,并没有和浏览器有任何交互. 也就是我们还不能制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM和DO ...
- Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射?
NET[C#]Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射? 问题描述 比如有如下的数据表结构:Person: person_id int first_name va ...
- SQLServer 表连接时使用top 1 去除重复数据
left join SM_SOLine soline on soline.SO=so.ID and soline.DocLineNo=(select MAX(DocLineNo) from SM_SO ...
- 移动端 fixed 固定按钮在屏幕下方,然后按钮被键盘顶上来...顶上来了有没有~
在移动端 H5 页面开发中,我使用了 fixed 固定某个元素在屏幕的最下方, 这时点击输入框,接着非常非常自然地出现了元素被系统键盘顶起来的情况,如下图. 解决方案: 首先,给页面最外层包裹一层 d ...
- INFORMIX MATCHES的使用详解
MATCHES 运算符返回 TRUE ,如果一个字符串与给定的掩码匹配. 语法 expr [NOT] MATCHES mask [ ESCAPE "char" ] ex ...
- 使用Flask和Bootstrap构建博客系统(1) - 准备篇
技术栈 macOS10.12.5 Python2.7.13 Bootstrap4.0.0-beta.2 virtualenv virtualenvwrapper 安装Python2.7.13 下载Bo ...
- jq+mui 阻止事件冒泡
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- ubuntu下svn up 出现 Can't convert string from 'UTF-8' to native encoding
root@ubuntu:/data/www# svn up svn: warning: cannot set LC_CTYPE locale svn: warning: environment var ...