(转)Overview : 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中,必须显示注明.如果你通过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#书写脚本,布布扣,bubuko.com

时间: 2024-11-03 01:59:31

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

Unity3D脚本手册

翻译:脚本_树 This is a short overview of how scripting inside Unity works. Scripting inside Unity consists of attaching custom script objects called behaviours to game objects. Different functions inside the script objects are called on certain events. Th

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 administrative operation. For more information about the mongo shell, see the Running .js files via a mongo shell Instance on the Server section for mo

关于Android import-module 和NDK_MODULE_PATH

=========================== 一.将NDK代码模块化 二.导出一个预编译库 =========================== 一.将NDK代码模块化 Android module paths (sharing code made easy): Android模块路径(方便共享代码): ============================================== Starting from r5, the Android NDK comes with

Git手册

GitUserManualChinese - Robin Wiki GitUserManualChinese Git 用户手册(1.5.3 及后续版本适用) 翻译: 罗峥嵘 (Robin Steven) < [email protected] > 英文版本: http://www.kernel.org/pub/software/scm/git/docs/user-manual.html Contents Preface 前言 Chapter 1. Repositories and Branch

Postman 基本操作学习

History 所有使用postman发送的request都会保存在这里.点击之后会在当前Tab打开. 参考: Requests History Environments 这里用来设定当前request 发送时使用的环境,比如这里可以选择“Cooper-Remote.template”或者“header_encode_style”或者“test_demo”,还可以选择“No Environment”,表示当前request不使用任何环境. 环境其实就是多组 key-value .环境可以被下载保

七、再啰嗦几句

Gotchas 本文档试图帮助您使用Blender API时在哪些地方可能会带来麻烦,避免一些不稳定性的操作. 1  Using Operators 使用运算符 Blender的操作人员是供用户访问的工具,Python也可以访问它们,但操作有一些限制,这些限制会使它们用起来很麻烦. 这些限制主要有... 不能传递诸如物体.网格或材料等数据来操作(只能操作上下文数据) 操作符的返回是否操作成功(如果它完成了或者被取消了),在某些情况下,从API的角度来返回操作的结果会更符合逻辑. 运营商的投票功能

a byte of vim -- 学习摘要

说在前面的话 -- a byte of vim 是我见过的最介绍vim 最好的书,想了解强大的vim的人,或者是已经在使用vim而打算进一步了解的人,我感觉都应该看看这个,内容精炼但涵盖很广,--"vim为什么这么强大",当看完这本书时,我想你就能完全的了解了--此外,本书是一英文版,但很简单,学语言的同时学习英语很不多-- 附上最新版的下载链接 http://download.csdn.net/detail/wuzhimang/8933257  ,当然上官网才是硬道理 以下是自己针对

free CSS and HTML5 site templates

http://templated.co/ 模板 A collection of 850 free CSS and HTML5 site templates, designed & built by Cherry + AJ and released under the Creative Commons https://tampermonkey.net/ Tampermonkey : Tampermonkey is a free browser extension and the most popu

Java文法(5)—Identifier

---------------------------------------------------------------------------------------------------------------- 说明: An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter. ------------