运行 Unity 脚本会按预定顺序执行大量事件函数。本页面将介绍这些事件函数,并说明它们的执行顺序。

脚本生命周期概述

下图概括了 Unity 如何在脚本的生命周期内对事件函数进行排序以及重复执行这些事件函数。

有关各种事件函数的更多信息,请参阅以下部分:

脚本生命周期流程图














































<inkscape:path-effect
effect="powerstroke"
id="path-effect7990"
is_visible="true"
offset_points="0,0.46875345"
sort_points="true"
interpolator_type="CubicBezierJohan"
interpolator_beta="0.2"
start_linecap_type="zerowidth"
linejoin_type="extrp_arc"
miter_limit="4"
end_linecap_type="zerowidth" />






















































































































<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="1"
inkscape:pageshadow="2"
inkscape:window-width="2481"
inkscape:window-height="1376"
id="namedview7936"
showgrid="false"
inkscape:snap-bbox="true"
inkscape:measure-start="0,0"
inkscape:measure-end="0,0"
inkscape:zoom="1"
inkscape:cx="227.98049"
inkscape:cy="364.23863"
inkscape:window-x="79"
inkscape:window-y="27"
inkscape:window-maximized="1"
inkscape:current-layer="layer7"
showguides="false"
inkscape:guide-bbox="true"
inkscape:snap-intersection-paths="false"
inkscape:object-paths="true"
inkscape:snap-smooth-nodes="false"
inkscape:object-nodes="true"
inkscape:snap-bbox-midpoints="false"
showborder="true"
inkscape:showpageshadow="false"
inkscape:pagecheckerboard="true"
borderlayer="false"
units="in"
fit-margin-top="15"
fit-margin-left="15"
fit-margin-right="15"
fit-margin-bottom="15">
<sodipodi:guide
position="498.58386,1474.7039"
orientation="1,0"
id="guide1286"
inkscape:locked="false" />
<sodipodi:guide
position="195.21417,2045.3078"
orientation="1,0"
id="guide1655"
inkscape:locked="false" />
<sodipodi:guide
position="606.32897,878.66804"
orientation="1,0"
id="guide1657"
inkscape:locked="false" />
<sodipodi:guide
position="390.83874,878.66804"
orientation="1,0"
id="guide2228"
inkscape:locked="false" />
<sodipodi:guide
position="1025.6188,383.80844"
orientation="1,0"
id="guide7992"
inkscape:locked="false" />
<sodipodi:guide
position="53.618757,1321.2569"
orientation="1,0"
id="guide8027"
inkscape:locked="false" />
<sodipodi:guide
position="647.98685,1486.1369"
orientation="1,0"
id="guide12328"
inkscape:locked="false" />
<sodipodi:guide
position="68.438015,2313.767"
orientation="1,0"
id="guide13909"
inkscape:locked="false" />
<sodipodi:guide
position="264.84573,2220.9539"
orientation="1,0"
id="guide13999"
inkscape:locked="false" />
</sodipodi:namedview>

rdf:RDF
<cc:Work
rdf:about="">
dc:formatimage/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
dc:title</dc:title>
</cc:Work>
</rdf:RDF>



















































































































Decommissioning


Physics


Initialization


Scene rendering


GUI rendering


Game logic


End of frame


Editor












Pausing


Input events


Initialization




Internal animation update













Gizmo rendering




Internal animation update





Internal animation update












































































yield WaitForFixedUpdate












Awake












OnEnable












FixedUpdate












Update












yield WaitForSeconds












yield null












LateUpdate












OnPreCull












OnGUI












yield WaitForEndOfFrame












yield WWW












yield StartCoroutine












OnWillRenderObject












OnBecameInvisible












OnBecameVisible












OnPreRender












OnRenderObject












OnPostRender












OnRenderImage











Reset











OnTriggerXXX












Internal physics update












OnCollisionXXX












OnDrawGizmos












OnApplicationPause












OnMouseXXX












OnApplicationQuit












OnDestroy












OnDisable




















Reset












Start





State machine update





OnStateMachineEnter/Exit





ProcessGraph





ProcessAnimation





Fire animation events






StateMachineBehaviour callbacks





OnAnimatorMove





OnAnimatorIK





WriteTransform





WriteProperties





State machine update





OnStateMachineEnter/Exit





ProcessGraph





ProcessAnimation





Fire animation events






StateMachineBehaviour callbacks





OnAnimatorMove





OnAnimatorIK





WriteTransform





WriteProperties






Reset is called when the script is attached and not in playmode.



The physics cycle may happen more than once per frame if
the fixed time step is less than the actual frame update time.


If a coroutine has yielded previously but is now due to
resume then execution takes place during this part of the
update.


OnGUI is called multiple time per frame update.


OnDisable is called only when the script was disabled during
the frame. OnEnable will be called if it is enabled again.


OnDrawGizmos is only called while working in the editor.


OnApplicationPause is called after the frame where the
pause occurs but issues another frame before actually pausing.


Start is only ever called once for a given script.






User callback





Internal multithreaded function





Internal function


Legend


加载第一个场景

场景开始时将调用以下函数(为场景中的每个对象调用一次)。

请注意,对于添加到场景中的对象,在为任何对象调用 Start 和 Update 等函数之前,会为_所有_ 脚本调用 Awake 和 OnEnable 函数。当然,在游戏运行过程中实例化对象时,不能强制执行此调用。

Editor

在第一次帧更新之前

对于场景资源中的对象,在为任何脚本调用 Update 等函数之前,将在所有脚本上调用 Start 函数。当然,在游戏运行过程中实例化对象时,不能强制执行此调用。

帧之间

更新顺序

跟踪游戏逻辑和交互、动画、摄像机位置等的时候,可以使用一些不同事件。常见方案是在 Update 函数中执行大多数任务,但是也可以使用其他函数。

通常,不应依赖为不同的游戏对象调用相同事件函数的顺序 - 除非顺序明确记录或可设置。(如果需要对播放器循环进行更细粒度的控制,可以使用 PlayerLoop API。)

不能指定为同一 MonoBehaviour 子类的不同实例调用事件函数的顺序。例如,一个 MonoBehaviour 的 Update 函数可能会在另一个游戏对象(包括其父级或子级游戏对象)上的相同 MonoBehaviour 的 Update 函数之前或之后调用。

可以指定一个 MonoBehaviour 子类的事件函数应在不同子类的事件函数之前调用(使用 Project Settings 窗口的 Script Execution Order 面板)。例如,如果有两个脚本,EngineBehaviour 和 SteeringBehaviour,可以设置 Script Execution Order,这样 EngineBehaviours 始终在 SteeringBehaviours 之前更新。

动画更新循环

Unity 评估动画系统时,将调用以下函数和 Profiler 标记。

有用的性能分析标记

脚本生命周期流程图中显示的某些动画函数不是可以调用的事件函数;它们是 Unity 处理动画时调用的内部函数。

这些函数具有 Profiler 标记,因此您可以使用 Profiler 查看 Unity 在帧中调用这些函数的时间。知道 Unity 调用这些函数的时间有助于准确了解所调用的事件函数的具体执行时间。

例如,假设在 FireAnimationEvents 回调中调用 Animator.Play。如果知道只有在执行状态机更新 (State Machine Update) 和流程图 (Process Graph) 函数后才会触发 FireAnimationEvents 回调,就可以预期动画剪辑会在下一帧播放,而不是马上播放。

Rendering

Note: These callbacks only work with the Built-in Render Pipeline.

协程

Update 函数返回后将运行正常协程更新。协程是一个可暂停执行 (yield) 直到给定的 YieldInstruction 达到完成状态的函数。 协程的不同用法:

销毁对象时

退出时

在场景中的所有活动对象上调用以下函数: