博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D_c#脚本注意要点
阅读量:5236 次
发布时间:2019-06-14

本文共 3615 字,大约阅读时间需要 12 分钟。

1. Inherit from MonoBehaviour

继承自MonoBehaviour

All behaviour scripts must inherit from MonoBehaviour (directly or indirectly). This happens automatically in script, 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继承(直接的或间接的).在中这个是自己主动完毕的,可是在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 Awake or Start function in C# or Boo.

JavaScript中放在函数之外的代码,在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中类名被隐式的设置为脚本的文件名称(不包括文件扩展名).在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的类的构造函数中写不论什么代码.

posted on
2017-06-27 10:44 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/mthoutai/p/7083908.html

你可能感兴趣的文章
向浏览器添加一条数据
查看>>
Hibernate3中generator标签使用
查看>>
Python网络爬虫
查看>>
java 数组的定义
查看>>
兼容ie8 rgba()用法
查看>>
《Java编程那点事儿》读书笔记(五)——System,Integer,Calendar,Random和容器
查看>>
【oracle】linux下卸载oracle11方法
查看>>
深究AngularJS——自定义服务详解(factory、service、provider)
查看>>
Leetcode 14——Longest Common Prefix
查看>>
设计模式学习笔记六:原型设计模式
查看>>
链表的各种函数
查看>>
POJ 1258 Agri-Net(最小生成树 Prim 模版题)
查看>>
会爆栈,dfs传参,只能传1个~~RMQLCA在线
查看>>
使用laravel搭建CURD后台页面
查看>>
CSS-font
查看>>
FFMpeg音频重采样和视频格式转
查看>>
eclipse中安装svn插件实现版本控制
查看>>
Orchard学习资料,适合入门上手
查看>>
HTML/CSS/Javascript代码在线压缩、格式化(美化)工具
查看>>
UMDH
查看>>