Awake & Start

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当作构造函数来使用。

Awake & Start
1 using UnityEngine;

2 using System.Collections;

3 

4 public class Example : MonoBehaviour {

5     private GameObject target;

6     void Awake() {

7         target = GameObject.FindWithTag("Player");

8     }

9 }
View Code

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.

  Awake & Start

 

当我们为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

你可能感兴趣的:(start)