Unity3D C#脚本开发学习
1. Inherit from 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创建脚本,系统模版已经包含了必要的定义.
C#
public class NewBehaviourScript : MonoBehaviour {...}
Boo
class NewBehaviourScript (MonoBehaviour):...
2. Use the Awake or Start function to do initialisation.What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.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函数初始化.JavaScript中放在函数之外的代码,在C#或Boo中必须置于Awake或Start函数里.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#. Coroutines have to have a return type of IEnumerator and you yield using yield return ... ; instead of just yield ... ;.
C#中协同程序有不同的句法规则,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 ; // Wait for two seconds // 等两秒
yield return new WaitForSeconds ();
}
}
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的类的构造函数中写任何代码.
只能理解一部分,刚开始学习,后面开始仔细研究。
Unity3D C#脚本开发学习的更多相关文章
- [原]Unity3D深入浅出 - 脚本开发基础(Scripts)
常用脚本事件: Update:每帧调用一次 Start:在第一次Update执行前调用 Awake:脚本实例在创建时调用 FixedUpdate:每个固定物理时间间隔调用一次 LateUpdate:每 ...
- Auto.js无障碍免root脚本开发学习
Auto.js 简单入门 官方文档:https://hyb1996.github.io/AutoJs-Docs/#/ https://blog.csdn.net/QiHsMing/article/de ...
- Photon + Unity3D 在线游戏开发 学习笔记(两)
本文和大家 和大家说说 Photon 解压后的目录结构 这里面最基本的我们 以后开发要用到的目录 就是 deploy目录,这个目录里 放的是要挂载的 server 当然我们的 server端也要放在 ...
- Selenium+C#自动化脚本开发学习
1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver driver = new FirefoxDriver(); //打开指定的URL地址 driver.Navigate ...
- 从一点儿不会开始——Unity3D游戏开发学习(一)
一些废话 我是一个windows phone.windows 8的忠实粉丝,也是一个开发者,开发数个windows phone应用和两个windows 8应用.对开发游戏一直抱有强烈兴趣和愿望,但奈何 ...
- 【Unity】1.3 Unity3D游戏开发学习路线
分类:Unity.C#.VS2015 创建日期:2016-03-23 一.基本思路 第1步--了解编辑器 首先了解unity3d的菜单,视图界面.这些是最基本的基础,可以像学word操作一样,大致能明 ...
- 驱动开发学习笔记. 0.07 Uboot链接地址 加载地址 和 链接脚本地址
驱动开发学习笔记. 0.07 Uboot链接地址 加载地址 和 链接脚本地址 最近重新看了乾龙_Heron的<ARM 上电启动及 Uboot 代码分析>(下简称<代码分析>) ...
- 【JMeter4.0学习(二)】之搭建openLDAP在windows8.1上的安装配置以及JMeter对LDAP服务器的性能测试脚本开发
目录: 概述 安装测试环境 安装过程 配置启动 配置搭建OpenLDAP 给数据库添加数据 测试查询刚刚插入的数据 客户端介绍 JMeter建立一个扩展LDAP服务器的性能测试脚本开发 附:LDAP学 ...
- unity3d游戏开发学习之使用3dmax创建导弹模型
在着手研究Unity3D的游戏开发时,3D模型能够考虑从unity的assets store去获取,也能够从网上搜索下载,同一时候咱们也能够尝试下自己动手去做一些简单的模型. 这里就依据unity3d ...
随机推荐
- Jmeter聚合报告分析
Label:每个 JMeter 的 element(例如 HTTP Request)都有一个 Name 属性,这里显示的就是 Name 属性的值 Average:平均响应时间--默认情况下是单个 Re ...
- 20169210《Linux内核原理与分析》第十周作业
第一部分:实验 进程的调度时机与进程的切换 操作系统原理中介绍了大量进程调度算法,这些算法从实现的角度看仅仅是从运行队列中选择一个新进程,选择的过程中运用了不同的策略而已. 对于理解操作系统的工作机制 ...
- 【转】Android应用开发allowBackup敏感信息泄露的一点反思
转载:http://blog.csdn.net/yanbober/article/details/46417531 1 背景 其实这篇文章可能有些小题大作,但回过头想想还是很有必要的,有点阴沟里翻船的 ...
- android开发:@SuppressLint( NewApi )
这个是android带的lint工具提示的,lint官方的说法是 Improving Your Code with lint,应该是帮助提升代码的 ,如果不想用的话,可以右键点工程,然后在androi ...
- 最简单的自定义适配器adapter
下面是一个非常简单的自定义适配器的总体源码,从这个源码入门,就可以慢慢学会适配器了 适配器的作用: 完成数据和界面控件的绑定,把数据绑定到界面的现实控件条目上(对于listView,应该是listVi ...
- 我的Shell + VIM配置
1. 安装powerline-shell 下载powerline-shell # cd /opt/ # git clone https://github.com/milkbikis/powerline ...
- 内核增加支持yaffs2错误问题汇总
Q1: fs/yaffs2/yaffs_mtdif2.c:xxx: error: 'struct xxx1' has no member named 'fun_xxx' A1:比对fun_xxx和st ...
- sqlmap
http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数 2.判断可以用那种SQL注 ...
- Task类(任务)
任务表示应完成的某个单元的工作.这个单元的工作可以在单独的线程中运行,也可以以同步方式启动一个任务,这需要等待主调用线程.使用任务不仅可以获得一个抽象层,还可以对底层线程进行很多控制. 1.启动任务 ...
- Python开发实战教程(8)-向网页提交获取数据
来这里找志同道合的小伙伴!↑↑↑ Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知 ...