Awake & Start

MonoBehaviour.Awake()

  Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag. Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.

  Awake is called when the script object is initialised, regardless of whether or not the script is enabled.

  可以将Awake当作构造函数来使用。

 using UnityEngine;
using System.Collections; public class Example : MonoBehaviour {
private GameObject target;
void Awake() {
target = GameObject.FindWithTag("Player");
}
}

MonoBehaviour.Start()

  Start在enable为true的时候,在第一次调update前会被调。

  Where objects are instantiated during gameplay, their Awake function will naturally be called after the Start functions of scene objects have already completed.

  

当我们为MonoBehavior定义了[ExecuteInEditMode]后,我们还需要关心Awake和Start在编辑器中的执行状况。

当该MonoBehavior在编辑器中被赋于给GameObject的时候,Awake, Start 将被执行。
    当Play按钮被按下游戏开始以后,Awake, Start 将被执行。
    当Play按钮停止后,Awake, Start将再次被执行。
    当在编辑器中打开包含有该MonoBehavior的场景的时候,Awake, Start将被执行。

参考:

1、file:///D:/Program%20Files%20(x86)/Unity/Editor/Data/Documentation/Documentation/ScriptReference/MonoBehaviour.Awake.html

2、file:///D:/Program%20Files%20(x86)/Unity/Editor/Data/Documentation/Documentation/ScriptReference/MonoBehaviour.Start.html

3、http://www.cnblogs.com/xpvincent/p/3178042.html

最新文章

  1. 响应式web设计之CSS3 Media Queries
  2. JS闭包导致循环给按钮添加事件时总是执行最后一个
  3. [WinAPI] API 2 [MessageBox API][消息框API]
  4. js的__proto__与propertype的关系
  5. (四)ubuntu学习前传—uboot中对Flash和DDR的管理
  6. jquery delegate
  7. 从零开始学习jQuery (十) jQueryUI常用功能实战
  8. qt 多线程之间通讯
  9. PMP和PRINCE2的价值各是什么?PRINCE2的含金量如何?PMP和prince2有什么区别?
  10. CSS编码技巧
  11. 从零开始学习PYTHON3讲义(三)写第一个程序
  12. java并发编程实践——王宝令(极客时间)学习笔记
  13. c/c++ 用前序和中序,或者中序和后序,创建二叉树
  14. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D. High Load 构造
  15. js 二分搜索树删除子节点
  16. background 和渐变 总结
  17. JavaScript中的工厂函数
  18. HDU 6343 - Problem L. Graph Theory Homework - [(伪装成图论题的)简单数学题]
  19. setting.xml配置详解
  20. 如何在HPUX的终端提示符前显示当前登录用户信息和所在目录

热门文章

  1. 请求URL中有body怎么使用jmeter进行接口测试
  2. ng 动态的生成option。
  3. 拦截器springmvc防止表单重复提交【3】自己实际项目
  4. Java开发前期准备工作
  5. dataView 工具栏
  6. grep 命令使用指南
  7. php foreach 跳出本次循环/当前循环与终止循环的方法
  8. AngularJS:Select
  9. php中的move_uploaded_file
  10. Java中静态变量、静态代码块、非静态代码块以及静态方法的加载顺序