1. Inherit from 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创建脚本,系统模版已经包含了必要的定义.

C#

public class NewBehaviourScript : MonoBehaviour {...}

Boo

class NewBehaviourScript (MonoBehaviour):...

2. Use the Awake or Start function to do initialisation.What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.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函数初始化.JavaScript中放在函数之外的代码,在C#或Boo中必须置于Awake或Start函数里.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#. Coroutines have to have a return type of IEnumerator and you yield using yield return ... ; instead of just yield ... ;.

C#中协同程序有不同的句法规则,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 ; // Wait for two seconds // 等两秒
yield return new WaitForSeconds ();
}
}

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的类的构造函数中写任何代码.

只能理解一部分,刚开始学习,后面开始仔细研究。

最新文章

  1. 设计爬虫Hawk背后的故事
  2. linux删除文件后沒有释放空间
  3. Android Studio使用教程-菜单(Edit)
  4. jasoncpp读取jason数据如何判断某一字段是否存在
  5. 初学Hibernate之Query扩展
  6. 推荐一款好用轻便的在线UML画图工具
  7. MVC 区分是哪按键提交FORM
  8. nginx源码分析——http模块
  9. SSH三大框架的各自工作流程
  10. windows 下进程池的操作
  11. Centos 7安装python3(PY3.6)
  12. Android 应用内悬浮控件实践总结
  13. pthread_cond_wait虚假唤醒
  14. Codeforces Round #552 (Div. 3) D题
  15. Select2 4.0.5 API
  16. 【转载】使用python库--Graphviz为论文画出漂亮的示意图
  17. python之模块2
  18. 使用Nodpad++正则替换
  19. DHCP Option43配置
  20. Linux 下 rt3070 无线网卡找不到 firmware 问题

热门文章

  1. Yii 安装二维码扩展Qrcode
  2. Linux下源码安装Nginx服务
  3. (转)tar 解压缩命令
  4. Java-SSI框架学习
  5. android-配置虚拟机Virtual device
  6. Matlab mex编程
  7. 动态规划入门——Eddy's research II
  8. Java IO流学习总结(转)
  9. mybatis13 resultMap
  10. windows向ubuntu过渡之常用编程软件安装