OnEnable, Awake, Start order

关于游戏中游戏对象是否处于active或者脚本是否为enabled 时Start和Awake函数的调用
基本上可以有四种情况:
1.game object 为active 但是脚本为disabled
此时当对象被创建时只有Awake函数会被立刻调用,OnEnable和 Start会在Enabled脚本后调用。
2.game object 为deactive但是脚本为enabled
此时当对象被创建时不会有函数被调用,当active物体之后会按照Awake OnEnable Start顺序调用函数。
3.game object 为avtive且脚本为enabled
此时当对象被创建时会按照Awake OnEnable Start顺序执行函数。
4.game object 为deactive且脚本为disabled
此时创建对象不会有函数调用,当active物体之后Awake函数会立即调用,enabled脚本后OnEnable和Start会接着被调用。
 
需要注意的是Awake和Start在一个游戏物体的生命周期中只调用一次,但是OnEnable会在每次激活脚本的时候再次执行。
如在游戏中先创建一个物体,激活其脚本,则此时Awake OnEnable Start会调用,将一个脚本的enabled设为false则其OnDisable会被调用
,再次激活时OnEnable又会被调用可Awake 和 Start则不会再调用。注意将一个游戏对象SetActive(false)时其绑定的脚本以及子脚本中的OnDisable也
会被调用,SetActive(true)时会调用OnEnable。

注1:

A simple test with 3.5.2 revealed, most concurrent functions (well,at least the ones I tested: Awake, Start, OnEnable,FixedUpdate/Update/LateUpdate) abide by the execution order definedfor the scripts. The execution order of OnLevelWasLoadedis notaffected by that, and therefore cannot beinfluenced by the user. This could be considered a bug.

The order of the four methods of a script related to initializationis always:

  • Awake()
  • OnEnable()
  • OnLevelWasLoaded() // (only on scene changes)
  • Start()

However, if your script was disabled in the firstplace(via Script.enabled=false), this order changes to:

  • OnLevelWasLoaded() // is now calledfirst, before Awake()!(only on scene changes)
  • Awake()
  • [OnEnable()/Start() are not executed until the script is actuallyenabled]

In addition, note that Awake() and OnEnable() calls areconnected/interleaved. Meaning, assuming a particular, user-definedexecution order A and B with A*<*B,

  • eachindividual script of type A will execute itsAwake(), immediately! followedby its OnEnabled()
  • thenall scripts of type B will do the same
  • then all OnLevelWasLoaded() willbe executed, in a (presumably) fixedbut unpredictable order(assuming this scene was freshly loaded - otherwise this step isskipped completely)
  • then all Start() will beexecuted, in the order A,B

In particular, this means that OnEnable() of type A will beexecuted before Awake()of type B, while OnEnable() of type B will beexecuted after Awake()of type A. This overview explains it more clearly:

  • Awake()of Type A, instance 1
  • OnEnable() of Type A, instance 1
  • Awake()of Type A, instance 2 // order of instances cannot beinfluenced
  • OnEnable() of Type A, instance 2 // order of instances cannot beinfluenced
  • Awake()of Type B
  • OnEnable() of Type B
  • OnLevelWasLoaded() of Type ? // order cannot be influenced
  • OnLevelWasLoaded() of Type ? // order cannot be influenced
  • Start()of Type A
  • Start()of Type B

EDIT: Hm, this is a total mess. If DontDestroyOnLoad() is activatedfor such a script, this will get even morecomplicated, and the order changes yet again to:

  • [Awake() is never called again, only the very first time]
  • OnEnable()
  • OnLevelWasLoaded() // as opposed to beingcalled before OnEnable(),when DontDestroyOnLoad()is not activated
  • [Start() is never called again, only the very first time]

EDIT2: In addition, whenDontDestroyOnLoad() is active, the user-defined execution orderis no longer abided by!, neitherby OnEnable(), nor by OnDestroyOnLoad().

EDIT3: WAH! I'm gonna stop testing now, this is a neverendingstory... As @Noisecrime noticed,there is actually another bug,where user-defined execution order isoverriden if the script has an OnEnable()function!

Script A has OnEnable, Script B has not:

  • Awake()of Type A
  • OnEnable() of Type A
  • Awake()of Type B

Script B has OnEnable, Script A has not:

  • Awake()of Type B
  • OnEnable() of Type B
  • Awake()of Type A // after Type B!
注2:
自定义不同脚本的执行顺序(这里有一个BUG,与注1EDIT3类似)
问题: Iuse a "framework" class that is set to run before all the otherscripts (-300). This class has an Awake method. I also use anothernormal monobehaviour which defines the OnEnablemethod.

Once the application runs, the OnEnable of the second monobehaviouris called before the awake of the framework class. Is this theintended behaviour?

解决方法:

From the sound of it, this is the 'OnEnable' bug with scriptexecution order. That is I suspect your 'framework' script does nothave an OnEnable method in it, in which case for some odd reason itwill get trumped in execution order by the scripts that do, despiteany ordering you add to the script execution.

To fix this issue, simply add an OnEnable method to your frameworkscript.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值