Updated Event Execution Order

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Changed the wording to make coroutine behaviour more obvious, along with the physics iteration stuffs)
m (Mentioned who Joachim is, in case you happen to not know)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==Game loop==
+
{| align="right"
 +
  | __TOC__
 +
  |}
 +
Author: [[User:Mike|Michael Garforth]]
 +
 
 +
''Please note that this article supersedes an [http://www.unifycommunity.com/wiki/index.php?title=Event_Execution_Order&diff=6500&oldid=6194 earlier version] that was written by Joachim Ante of Unity Technologies in a forum post.  This new article is more comprehensive and is more accurate overall.  However, as the information has been drawn from observation rather than inside knowledge, it may be that there are aspects that are incorrect.  If you discover inaccuracies, please describe your findings in the [[:Talk:Updated Event Execution Order|talk page]].''
 +
 
 +
 
 +
 
 +
=='''Game loop'''==
 +
The following all happens in a single loop, in the order specified
 +
Physics does not necessarily run each time, and can run multiple times
  
  
Line 18: Line 29:
 
===Update events===
 
===Update events===
 
The rest of the events are run once per game loop iteration:
 
The rest of the events are run once per game loop iteration:
 +
*'''OnMouseDown'''/'''OnMouseUp'''/'''OnMouseDrag'''/'''OnMouseEnter'''/'''OnMouseExit'''/'''OnMouseOver'''
 
*'''Update'''
 
*'''Update'''
 
*'''WaitForSeconds'''/'''yield'''/'''yield return 0''' ''(Coroutines which yielded one of these will resume execution here, with WaitForSeconds resuming after the defined time)''
 
*'''WaitForSeconds'''/'''yield'''/'''yield return 0''' ''(Coroutines which yielded one of these will resume execution here, with WaitForSeconds resuming after the defined time)''
 
*'''LateUpdate'''
 
*'''LateUpdate'''
 
  
 
===Rendering===
 
===Rendering===
Line 30: Line 41:
 
*'''OnRenderObject'''
 
*'''OnRenderObject'''
 
*'''OnPostRender'''
 
*'''OnPostRender'''
*'''OnRenderImage''' ''(pro only)''
+
*'''OnRenderImage''' ''(Pro only)''
  
  
*'''OnGUI'''
+
*'''OnGUI''' ''(Called multiple times. Layout then Repaint events are called first, followed by a Layout and keyboard/mouse event for each input event)''
*'''OnDrawGizmos''' (Only when scene is drawn, even if object is inactive)
+
*'''OnDrawGizmos''' ''(Only when scene is drawn, even if object is inactive)''
  
  
Line 43: Line 54:
  
  
==First scene load==
+
=='''First scene load'''==
  
  
 
Repeated once per object:
 
Repeated once per object:
 
*'''Awake'''
 
*'''Awake'''
*'''OnEnable''' ''(if the object is active)''
+
*'''OnEnable''' ''(If the object is active)''
  
  
Line 57: Line 68:
  
  
==After calling Application.LoadLevel==
+
=='''After calling Application.LoadLevel'''==
  
  
Line 69: Line 80:
 
Repeated once per scene object:
 
Repeated once per scene object:
 
*'''Awake'''
 
*'''Awake'''
*'''OnEnable''' ''(if the object is active)''
+
*'''OnEnable''' ''(If the object is active)''
  
  
Line 83: Line 94:
  
  
==When quitting==
+
=='''When quitting'''==
 
For all active objects:
 
For all active objects:
 
*'''OnApplicationQuit'''
 
*'''OnApplicationQuit'''
Line 91: Line 102:
  
  
==Immediate events==
+
=='''Immediate events'''==
 
These are all called immediately when certain functions are called (e.g. Instantiate, setting enabled):
 
These are all called immediately when certain functions are called (e.g. Instantiate, setting enabled):
 
*'''Awake''' - called during MonoBehaviour instantiation
 
*'''Awake''' - called during MonoBehaviour instantiation
 
*'''OnEnable'''/'''OnDisable''' - OnEnable called during MonoBehaviour instantiation
 
*'''OnEnable'''/'''OnDisable''' - OnEnable called during MonoBehaviour instantiation
 
*'''Reset''' - called when the script is reset ''(Editor Only)''
 
*'''Reset''' - called when the script is reset ''(Editor Only)''

Latest revision as of 21:02, 21 October 2009

Contents

Author: Michael Garforth

Please note that this article supersedes an earlier version that was written by Joachim Ante of Unity Technologies in a forum post. This new article is more comprehensive and is more accurate overall. However, as the information has been drawn from observation rather than inside knowledge, it may be that there are aspects that are incorrect. If you discover inaccuracies, please describe your findings in the talk page.


[edit] Game loop

The following all happens in a single loop, in the order specified Physics does not necessarily run each time, and can run multiple times


[edit] After an instantiation

For objects that were instantiated last game loop iteration:

  • Start


[edit] Physics loop

Physics is run in a loop, iterating over the following until physics has caught up to the current frame:

  • FixedUpdate
  • Physics simulation
  • OnCollisionEnter/OnCollisionStay/OnCollisionExit
  • OnTriggerEnter/OnTriggerStay/OnTriggerExit
  • WaitForFixedUpdate (Coroutines which yielded WaitForFixedUpdate will resume execution after the next physics iteration completes)


[edit] Update events

The rest of the events are run once per game loop iteration:

  • OnMouseDown/OnMouseUp/OnMouseDrag/OnMouseEnter/OnMouseExit/OnMouseOver
  • Update
  • WaitForSeconds/yield/yield return 0 (Coroutines which yielded one of these will resume execution here, with WaitForSeconds resuming after the defined time)
  • LateUpdate

[edit] Rendering

  • OnPreCull
  • OnBecameVisible/OnBecameInvisible
  • OnWillRenderObject
  • OnPreRender
  • OnRenderObject
  • OnPostRender
  • OnRenderImage (Pro only)


  • OnGUI (Called multiple times. Layout then Repaint events are called first, followed by a Layout and keyboard/mouse event for each input event)
  • OnDrawGizmos (Only when scene is drawn, even if object is inactive)


[edit] Before the frame ends

  • WaitForEndOfFrame (Coroutines which yielded WaitForEndOfFrame will resume execution before the next game loop finishes)



[edit] First scene load

Repeated once per object:

  • Awake
  • OnEnable (If the object is active)


At the beginning of the next frame, per object:

  • Start



[edit] After calling Application.LoadLevel

[edit] Before the level loads

Repeated once per active object, after the end of the current frame:

  • OnDisable


[edit] Level loads

[edit] After level loads

Repeated once per scene object:

  • Awake
  • OnEnable (If the object is active)


Repeated once per active DontDestroyOnLoad object from last level:

  • OnEnable


Repeated once per active object:

  • OnLevelWasLoaded



[edit] When quitting

For all active objects:

  • OnApplicationQuit
  • OnDisable



[edit] Immediate events

These are all called immediately when certain functions are called (e.g. Instantiate, setting enabled):

  • Awake - called during MonoBehaviour instantiation
  • OnEnable/OnDisable - OnEnable called during MonoBehaviour instantiation
  • Reset - called when the script is reset (Editor Only)
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox