码迷,mamicode.com
首页 > 其他好文 > 详细

Awake & Start

时间:2014-06-26 00:37:37      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   tar   

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

bubuko.com,布布扣
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.

  bubuko.com,布布扣

 

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

Awake & Start,布布扣,bubuko.com

Awake & Start

标签:style   class   blog   code   http   tar   

原文地址:http://www.cnblogs.com/tekkaman/p/3804290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!