日期:2014-05-18 浏览次数:21123 次
转载自?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