1. Inherit from MonoBehaviour

继承自MonoBehaviour

All 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#脚本注意要点的更多相关文章

  1. 使用loadrunner录制脚本的思路和注意要点

    基本思路如下图: 注意要点有如下几点: 1.性能测试往往需要准备大批量的数据,大批量数据的生成方法有很多种,常见的有: (1)编写SQL语句来插入数据 (2)使用DataFactory等专业的数据生成 ...

  2. JavaScript HTML5脚本编程——“历史状态管理”的注意要点

    历史状态管理是现代Web应用开发中的一个难点.在现代Web应用中,用户的每次操作不一定会打开一个全新的页面,因此"后退"和"前进"按钮也就失去了作用,导致用户很 ...

  3. zabbix增加手机短信、邮件监控的注意要点,SSL邮件发送python脚本

    1.短信接口文档: URL http://xxx.com/interfaces/sendMsg.htm Method POST Description 文字短信调用接口 Request Param L ...

  4. Apache执行Python脚本

    由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...

  5. SQL Server镜像自动生成脚本

    SQL Server镜像自动生成脚本 镜像的搭建非常繁琐,花了一点时间写了这个脚本,方便大家搭建镜像 执行完这个镜像脚本之后,最好在每台机器都绑定一下hosts文件,不然的话,镜像可能会不work 1 ...

  6. 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)

    分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...

  7. 探真无阻塞加载javascript脚本技术,我们会发现很多意想不到的秘密

    下面的图片是我使用firefox和chrome浏览百度首页时候记录的http请求 下面是firefox: 下面是chrome: 在浏览百度首页前我都将浏览器的缓存全部清理掉,让这个场景最接近第一次访问 ...

  8. 第一个shell脚本

    打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...

  9. Java 8 的 Nashorn 脚本引擎教程

    本文为了解所有关于 Nashorn JavaScript 引擎易于理解的代码例子. Nashorn JavaScript 引擎是Java SE 8的一部分,它与其它像Google V8 (它是Goog ...

随机推荐

  1. 【SDOI 2010】 古代猪文

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1951 [算法] 欧拉定理+中国剩余定理 + lucas定理 [代码] #includ ...

  2. FH Admin

    http://www.360doc.com/content/14/0713/08/8072791_394027312.shtml

  3. [Plugin] JQuery.uploadify上传文件插件的使用详解For ASP.NET

    URL:http://www.cnblogs.com/xiaopin/archive/2010/01/21/1653523.html 今天下午整理文件上传的例子,感觉收集到的例子都很不人性话,后来找到 ...

  4. NOIP2013 D2T1 积木大赛

    [NOIP2013T4]积木大赛 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 noip2013day2 描述 春春幼儿园举办了一年一度的"积木大 ...

  5. CSS3实现简单的幻灯片

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 谷歌vimium配置

    谷歌不得不说真的是一个非常好用的浏览器(之前用的浏览器真的好low),推荐一款非常极客的的插件vimium,让你使用彻底告别鼠标,瞬间感觉高大上... 默认配置: j: 向下细微滚动窗口.  k:向上 ...

  7. hdu 2768 Cat vs. Dog 最大独立集 巧妙的建图

    题目分析: 一个人要不是爱狗讨厌猫的人,要不就是爱猫讨厌狗的人.一个人喜欢的动物如果离开,那么他也将离开.问最多留下多少人. 思路: 爱猫和爱狗的人是两个独立的集合.若两个人喜欢和讨厌的动物是一样的, ...

  8. hdu2112 HDU Today 基础最短路

    这题的关键是把车站的名字转化为点的编号.我用的是map.声明一个map<string,int> st,然后按照字符串出现的次序给st赋值.例如:st[s1]=2;代表这字符串s1出现的次序 ...

  9. 使用Caffe预测遇到的问题

    1. 在使用网络预测图像时, prediction = net.predict( [input_image] ) 出现: net.image_dims[0] 不是整数情况, (2).甚至以为np.ze ...

  10. Qt:&OpenCV—Q图像处理基本操作(Code)

    原文链接:http://www.cnblogs.com/emouse/archive/2013/03/31/2991333.html 作者写作一系列:http://www.cnblogs.com/em ...