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

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 }

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

Awake & Start,布布扣,bubuko.com

时间: 2024-08-24 18:36:08

Awake & Start的相关文章

【酷熊科技】工作积累 ----------- Unity3d中的Awake()、OnEnable()、Start()等默认函数的执行顺序和生命周期

Awake()在MonoBehavior创建后就立刻调用,在脚本实例的整个生命周期中,Awake函数仅执行一次:如果游戏对象(即gameObject)的初始状态为关闭状态,那么运行程序,Awake函数不会执行:如果游戏对象的初始状态为开启状态,那么Awake函数会执行:值得注意的一点是,Awake函数的执行与否与脚本实例的状态(启用或禁用)并没有关系,而是与脚本实例所绑定的游戏对象的开关状态有关.如果重新加载场景,那么场景内Awake函数的执行情况重新遵循上述两点. Start()将在MonoB

[Unity3D]脚本中Start()和Awake()的差别

Unity3D刚開始学习的人常常把Awake和Start混淆. 简单说明一下,Awake在MonoBehavior创建后就立马调用,Start将在MonoBehavior创建后在该帧Update之前.在该Monobehavior.enabled == true的情况下运行. [javascript] view plaincopy void Awake (){ } //初始化函数,在游戏開始时系统自己主动调用.一般用来创建变量之类的东西. void Start(){ } //初始化函数,在全部Aw

wake,awake,waken,awaken的区别

wake,awake,waken,awaken的区别 这4个单词都有"醒来,唤醒,弄醒"的意意思,但又有不同之处. 1:wake,作及物动词和不及物动词的时候都常与up连用.a:I wake up at six every morning.我每天早上六点醒来. b:Please wake me up at six in the morning.请早上六点钟叫醒我. 2:awake,它虽然也有用作动词,表示“醒来,唤醒,觉醒"的意思,但是我们大部分情况都把这个单词用它的形容词词

Unity and C#: Game Loop (Awake, Start, Update)

Introduction The central component of any game, from a programming standpoint, is the game loop. It allows the game to run smoothly regardless of a user's input or lack thereof. Every game must and should have a game loop because a game must continue

Unity中Awake和Start的区别

正式开始学习Unity了.当然,第一个遇到的问题就是Awake和Start的问题,之前在网上查过一下这两者的区别,简单记忆了一下,认为自己知道了两者的区别.不过实际用起来,发现对于这两者到底是什么区别,心里还是没底,而且最关键的是木有Unityt的源代码,所以我们只能是通过文档或者是别人的blog来了解,当然,还有一个办法就是自己做一下实验,实践是检验真理的唯一标准. 一.官方解释 先来看看Unity官方对于这两个函数的解释: Awake is called when the script in

PatentTips - Fast awake from low power mode

BACKGROUND Electronic devices, such as electronic book readers ("eBook reader devices"), cellular telephones, portable media players, desktop computers, laptops, tablet computers, netbooks, personal digital assistants, and the like, rely on elec

Tip8:Unity中诸如 Awake() Start() Update()等函数的 执行顺序

Unity脚本中有很多的事件函数,下面是各种函数的执行顺序: 1.reset(); 2.Awake(); 3.OnEnable; 4.OnLevelWasLoaded(); 5.Start(); 6.OnApplicationPause(); 7.FixedUpdate(); 8.Update(); 9.LateUpdate(); 10.Rendering(渲染)类 11.Coroutines(协调程序)类 12.OnDestroy(); 13.OnApplicationQuit(); 14.O

I am still awake

Now is already pass 1am and I am still awake cause it is holiday tomorrow. Soon will be bed time for me. Snart ska det vara natten, Och jag ska hoppa i sängen. Söt dröm, Lång sömn. Och morgonen väntar på mig. by Kitty

Unity3D脚本中的Awake()和Start()的本质区别

昨天被问到一个问题MonoBehaviour的Awake()和Start()的区别是啥? 当然Awake()会在Start()之前被调用,这个地球人都知道了.如果只是顺序问题,那当然就没太大必要搞两个函数了.仔细研究了一下API文档: Awake():Awake is called when the script instance is being loaded. Start():Start is called on the frame when a script is enabled just