基本上可以有四种情况:
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。
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
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
- 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
- [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:
EDIT3: WAH! I'm gonna stop testing now, this is a neverendingstory... As
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!
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.