Apart from syntax, there are some differences when writing scripts in C# or Boo. Most notable are:

除了句法规则, 使用C#或Boo编写脚本还有一些不同,当.需要特别注意的是:

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 Awakeor Startfunction in C# or Boo.

JavaScript中放在函数之外的代码,在C#或Boo中必须置于AwakeStart函数里.

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

(转)Overview : Writing Scripts in C# 使用C#书写脚本的更多相关文章

  1. MongoDB - The mongo Shell, Write Scripts for the mongo Shell

    You can write scripts for the mongo shell in JavaScript that manipulate data in MongoDB or perform a ...

  2. 3.Write Scripts for the mongo Shell-官方文档摘录

    总结 1 使用js进行获取数据的方法 2 js方式和原生mongo shell的交互方式的区别写法 3 需要将所有数据打印出来使用到的循环示例 cursor = db.collection.find( ...

  3. 19 Zabbix 利用Scripts栏目对Hosts远程执行命令

    点击返回:自学Zabbix之路 19 Zabbix 利用Scripts栏目对Hosts远程执行命令 在Monitoring板块中,有Host出现的地方,单击Host按钮后,都可以执行对Host远程执行 ...

  4. 20 Zabbix 利用Scripts栏目对Hosts远程执行命令

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 20 Zabbix 利用Scripts栏目对Hosts远程执行命令 在Monitoring板块中, ...

  5. /etc目录深入理解

    /etc This is the nerve center of your system, it contains all system related configuration files in ...

  6. Postman 基本操作学习

    History 所有使用postman发送的request都会保存在这里.点击之后会在当前Tab打开. 参考: Requests History Environments 这里用来设定当前reques ...

  7. Elasticsearch Painless语言(实现搜索打分基础)

    With the release of Elasticsearch 5.x came Painless, Elasticsearch's answer to safe, secure, and per ...

  8. [ZZ]From QA to Engineering Productivity

    http://googletesting.blogspot.com/2016/03/from-qa-to-engineering-productivity.html In Google’s early ...

  9. (copy) Top Ten Reasons not to use the C shell

    http://www.grymoire.com/Unix/CshTop10.txt ========================================================== ...

随机推荐

  1. [ 原创 ]新手作品-我的第一款安卓自学作品---YY音乐诞生了

    YY音乐---我前前后后断断续续花了3个月时间,边从0基础开始学Android 和Java,边开始做自己的Android第一款软件,之间看了许多教学视频,许多博客,才算Android入了一点点门道,终 ...

  2. redis keys 命令

    ## 删除存在的key del key ## 序列体弱给定key,并返回被序列化的值 dump key ## 检查key是否存在 exists key ## 为给定key设置过期时间 expire k ...

  3. BZOJ 1951SDOI2010 古代猪文

    真是到很强的数学题 先利用欧拉定理A^B %p=A^(B%φ(p)+φ(p) ) %p 然后利用卢卡斯定理求出在modφ(p)的几个约数下的解 再利用中国剩余定理合并 计算答案即可 By:大奕哥 #i ...

  4. 【8.13校内测试】【DP】【按除数分类】【二分】

    感觉今天状态不太好啊一大早就很困,t1卡得有点久,以为三道题都是这个难度,结果难度完全是倒着排的啊!!在dp和数学上还得多练题!! 很像背包的一道DP??先不考虑树的结构,给每个点都先分配一个度数,剩 ...

  5. 腾讯的网站是如何检测到你的 QQ 已经登录?

    在 QQ 已经登录的情况下,手动输入网址打开 QQ 邮箱 或者 QQ 空间 等腾讯网站,可以看到网页已经检测到本地 QQ 客户端已经登录,于是用户可以很方便地一键登录网站而不必再输入用户名密码. 这实 ...

  6. struts2对拦截器使用带实例

    拦截器是struts2的核心.拦截器可以拦截请求,控制视图的走向.那么怎么来实现自定义的拦截器呢? 这里我们做一个例子. 首先假现在做了两个jsp页面一个是登陆的信息的(用session来模拟),一个 ...

  7. 【docker-compose】使用docker-compose部署运行spring boot+mysql 【处理容器的时区问题】【详解】【福利:使用docker-compose构建 wordpress+mysql】

    ==================================================================================================== ...

  8. UCOS源码剖析 (一)

    UCOS源码详解 uC/OS-II源码分析(总体思路 一) 首先从main函数开始,下面是uC/OS-II main函数的大致流程: main()      { OSInit(); TaskCreat ...

  9. Spring注解方式实现任务调度【官方文档翻译】

    原文:http://docs.spring.io/spring/docs/4.0.1.BUILD-SNAPSHOT/javadoc-api/ 注解类型:EnableScheduling @Target ...

  10. Android图片加载框架最全解析(六),探究Glide的自定义模块功能

    不知不觉中,我们的Glide系列教程已经到了第六篇了,距离第一篇Glide的基本用法发布已经过去了半年的时间.在这半年中,我们通过用法讲解和源码分析配合学习的方式,将Glide的方方面面都研究了个遍, ...