转载自 19920105
Writing Scripts in C#
使用C#写脚本
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就必须明确地指定其继承于MonoBehaviour。如果你是在u3d中通过“Asset->Create->C Sharp Script/Boo Script”来创建了脚本代码文件的话,u3d的脚本创建模板将会提前将相关继承语句定义在脚本代码文件中。
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.
,你(需要)在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脚本文件中,u3d虽然没有明确地定义主类,但事实上,u3d已经隐性地自动定义了主类,并将类名设置为等于脚本文件名(不包括扩展名)。
如果使用的是C#a或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
(U3D中的)协同会(同时)用一个属于IEnumerator接口类型(枚举)的返回值和你使用的yield 返回值...;来替代yield......;
如下面代码:
using System.Collections;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
// C# coroutine
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.
U3D目前不支持你在脚本中使用命名空间,这个需求会在未来的版本中实现。
6. Only member variables are serialized and are shown in the Inspector.
只有(public公有的)成员变量是可以在U3D程序的Inspector栏中会被以序列形式显示出来
Private and protected member variables are shown only in Expert Mode. Properties are not serialized or shown in the inspector.
私有类型(private)和成员类型(protected)变量只能在专家模式(Expert Mode)下可见,(而且)属性(Properties)
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.
不要通过构造函数来初始化变量。这些工作可以使用第2条中的Awake方法和Start方法来替代(换句话来说就是在u3d中,Awake方法和 Start方法是每个脚本文件类中默认的构造函数)。U3D甚至可以在标准编辑模式下就调用它们。它们通常是直接汇编在脚本中,因为构造函数需要检索默认 脚本变量用于引用。(u3d)在任意的时候不光可以调用构造函数,还可能会调用预设(物体)或未被唤醒的游戏物体。
In the case of eg. a singleton pattern using the constructor this can have severe consequences and lead to seemingly random null reference exceptions.
实例化(C#脚本文件)时,单脚本文件状态下使用(自定义的)构造函数(可能)会导致严重的后果,并且会产生引用为空的异常。
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.
所以,如果你实例化C#脚本文件(即运行C#脚本文件。这是c#程序运行的基本方式,详细内容可以从C#专门的教材中了解),单脚文件不要使用(自定义 的)的构造函数,直接使用Awake方法替代即可,实在没有理由为一个继承之MonoBehaviour的(文件)类写任何的(构造函数)代码。
分享到:
相关推荐
Unity 3D脚本编程:使用C#语言开发跨平台游戏以Unity 3D的跨平台基础Mono以及其游戏脚本语言C#为基础进行讲解
Unity 3D脚本编程-使用C#语言开发跨平台游戏
Unity3D C#脚本中文教程
Unity3d脚本编程 使用C#语言开发跨平台游戏,适用于新手,本书全面系统地介绍了基于C#的Unity 3D跨平台开发,涉及Unity 3D 跨平台原理分析、Unity 3D 和Mono 的结合,以及在游戏脚本编程中使用C#语言
Unity 3D脚本编程 使用C#语言开发跨平台游戏_PDF,Unity 3D脚本编程 使用C#语言开发跨平台游戏_PDF
这是Unity3d的脚本大总结,很多很有用的脚本,包括js以及c#,还有一些操作方法。
本资源讲述unity3D利用C#脚本进行编程,对C#脚本进行详细讲解
让不懂编程的人爱上Unity3d游戏开发026-Unity3d和C#的双剑合璧01-Unity游戏脚本剖析上.pdf让不懂编程的人爱上Unity3d游戏开发026-Unity3d和C#的双剑合璧01-Unity游戏脚本剖析上.pdf让不懂编程的人爱上Unity3d游戏...
Unity 3D脚本编程-使用C#语言开发跨平台游戏
Unity3D C#脚本中文教程.rar
Unity3D脚本的808个教程,用于了解unity3d,game制作的入门教程
Unity3D脚本:C#计时类脚本1
unity 3d是目前非常流行的游戏开发平台,本书主要介绍使用c#开发游戏,有兴趣的朋友可以下载来看一看
当你学习Unity3D书写脚本困难的时候,看一下
Unity3D中任意类型数据转换成json数据的方法 Posted on 2013年03月20日 by U3d / Unity3D脚本/插件/被围观 275 次
高清版本带目录,里面的内容非常精华,对unity和c#的底层开发和跨平台做了很详细的分析。
unity3d C#转JS 非常不错的脚本