
在Unity腳本中,有一些按照預(yù)定順序執(zhí)行的事件函數(shù),腳本即是按照此順序執(zhí)行的。這個執(zhí)行順序描述如下:
First Scene Load 第一個場景加載
These functions get called when a scene starts (once for each object in the scene).
這些函數(shù)在場景開始時就被調(diào)用了(對場景中的每個對象執(zhí)行一次)。
Awake:This function is always called before any Start functions and also just after a prefab is instantiated.
?Awake:這個函數(shù)是在Start函數(shù)之前以及預(yù)制物體實例化后被調(diào)用。
OnEnable:(only called if the Object is active): This function is called just after the object is enabled.
OnEnable:(僅在對象激活狀態(tài)下可用):這個函數(shù)在對象可用之后被調(diào)用。
Before the first frame update 第一幀更新之前
Start:Start is called before the first frame update only if the script instance is enabled.
Start:如果腳本實例化被啟用,則Start函數(shù)在第一幀更新之前被調(diào)用。
In between frames 執(zhí)行期間
OnApplicationPause:This is called at the end of the frame where the pause is detected, effectively between the normal frame updates. One? ? ? ?extra frame will be issued after OnApplicationPause is called to allow the game to show graphics that indicate the paused state.
OnApplicationPause:若暫停被檢測到,當(dāng)前幀執(zhí)行后就調(diào)用此函數(shù),在正常的運行期間調(diào)用是有效的。在OnApplicationPause被調(diào)用后,額外的用一幀來顯示圖像表明暫停狀態(tài)。
Update Order 更新順序
When you're keeping track of game logic and interactions, animations, camera positions, etc., there are a few different events you can use. The common pattern is to perform most tasks inside the Update() function, but there are also other functions you can use.
這里有一些不同的事件函數(shù)供你來追蹤游戲邏輯,動畫和相機(jī)的位置等。在Update()函數(shù)中執(zhí)行大多數(shù)任務(wù)是比較常見的,然而也有其他函數(shù)可以使用。
FixedUpdate:FixedUpdate() is often called more frequently than Update(). It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high. All physics calculations and updates occur? immediately after FixedUpdate(). When applying movement calculations inside FixedUpdate(), you do not need to multiply your values by Time.deltaTime.? This is because FixedUpdate() is called on a reliable timer, independent of the frame rate.
FixedUpdate: FixedUpdate()比Update()函數(shù)調(diào)用的更頻繁。當(dāng)幀率比較低時,它每幀被調(diào)用多次,如果幀率比較高,它有可能就不會被調(diào)用。所有的物理計算和更新都立即發(fā)生在FixedUpdate()之后。當(dāng)在FixedUpdate()中計算物體移動時,你不需要乘以Time.deltaTime。因為FixedUpdate()是基于可靠的定時器的,不受幀率的影響。
Update:Update() is called once per frame. It is the main workhorse function for frame updates.
Update()?每幀調(diào)用一次。這是幀更新的主要函數(shù)。
LateUpdate:LateUpdate() is called once per frame, after Update() has finished. Any calculations that are performed in Update() will have completed when LateUpdate() begins. A common use for LateUpdate() would be a following third-person camera. If you make your character move and turn inside Update(), you can perform all camera movement and rotation calculations in LateUpdate(). This will ensure that the character has moved completely before the camera tracks its position.
LateUpdate: 在Update()執(zhí)行后,LateUpdate() 也是每幀都被調(diào)用。在Update()中執(zhí)行的任何計算都會在LateUpdate()開始之前完成。LateUpdate()的一個常見應(yīng)用就是第三人稱控制器的相機(jī)跟隨。如果你把角色的移動和旋轉(zhuǎn)放在Update()中,那么你就可以把所有相機(jī)的移動旋轉(zhuǎn)放在LateUpdate()。這是為了在相機(jī)追蹤角色位置之前,確保角色已經(jīng)完成移動。
Rendering 渲染
OnPreCull:Called before the camera culls the scene. Culling determines which objects are visible to the camera. OnPreCull is called just before culling takes place.
OnPreCull:在相機(jī)剔除場景前被調(diào)用。剔除取決于物體在相機(jī)中是否可見。OnPreCull僅在剔除執(zhí)行之前被調(diào)用。
OnBecameVisible/OnBecameInvisible:Called when an object becomes visible/invisible to any camera.
OnBecameVisible/OnBecameInvisible:當(dāng)物體在任何相機(jī)中可見/不可見時被調(diào)用。
OnWillRenderObject:Calledoncefor each camera if the object is visible.
OnWillRenderObject:如果物體可見,它將為每個攝像機(jī)調(diào)用一次。
OnPreRender:Called before the camera starts rendering the scene.
OnPreRender: 在相機(jī)渲染場景之前被調(diào)用。
OnRenderObject:Called after all regular scene rendering is done. You can use GL class or Graphics.DrawMeshNow to draw custom geometry at this point.
OnRenderObject:在所有固定場景渲染之后被調(diào)用。此時你可以使用GL類或者Graphics.DrawMeshNow來畫自定義的幾何體。
OnPostRender:Called after a camera finishes rendering the scene.
OnPostRender: 在相機(jī)完成場景的渲染后被調(diào)用。
OnRenderImage(Pro only):Called after scene rendering is complete to allow postprocessing of the screen image.
OnRenderImage(僅專業(yè)版):在場景渲染完成后被調(diào)用,用來對屏幕的圖像進(jìn)行后處理。
OnGUI:Called multiple times per frame in response to GUI events. The Layout and Repaint events are processed first, followed by a Layout and keyboard/mouse event for each input event.
OnGUI: 每幀被調(diào)用多次用來回應(yīng)GUI事件。布局和重繪事件先被執(zhí)行,接下來是為每一次的輸入事件執(zhí)行布局和鍵盤/鼠標(biāo)事件。
OnDrawGizmos:Used for drawing Gizmos in the scene view for visualisation purposes.
OnDrawGizmos:為了可視化的目的在場景視圖中繪制小圖標(biāo)。
Coroutine 協(xié)同程序(協(xié)程)
Normal coroutine updates are run after the Update function returns. A coroutine is function that can suspend its execution (yield) until the given given YieldInstruction finishes. Different uses of Coroutines:
正常情況下協(xié)程是在Update函數(shù)返回時執(zhí)行。協(xié)程的功能是,延緩其執(zhí)行(yield) ,直到給定的YieldInstruction完成。協(xié)程的不同用途:
yield;The coroutine will continue after all Update functions have been called on the next frame.
yield:協(xié)程在所有的Update函數(shù)于下一幀調(diào)用后繼續(xù)執(zhí)行。
yield WaitForSeconds(2);Continue after a specified time delay, after all Update functions have been called for the frame
yield WaitForSeconds(2):在一個指定的時間延遲后繼續(xù)執(zhí)行,在所有的Update函數(shù)被調(diào)用之后。
yield WaitForFixedUpdate();Continue after all FixedUpdate has been called on all scripts
yield WaitForFixedUpdate():在所有腳本上所有的FixedUpdate被調(diào)用之后繼續(xù)執(zhí)行。
yield WWWContinue after a WWW download has completed.
yield WWW:在WWW加載完成之后繼續(xù)執(zhí)行。
yield StartCoroutine(MyFunc);Chains the coroutine, and will wait for the MyFunc coroutine to complete first.
yield StartCoroutine(MyFunc):用于鏈接協(xié)程,此將等待MyFunc協(xié)程完成先。
When the Object is Destroyed 當(dāng)對象被銷毀時
OnDestroy:This function is called after all frame updates for the last frame of the object's existence (the object might be destroyed in response to Object.Destroy or at the closure of a scene).
OnDestroy: 這個函數(shù)在所有幀更新之后被調(diào)用,在對象存在的最后一幀(對象將銷毀來響應(yīng)Object.Destroy或關(guān)閉一個場景)。
When Quitting 當(dāng)退出時
These functions get called on all the active objects in your scene, :
這些函數(shù)對于場景中的所有激活狀態(tài)的物體都會被調(diào)用。
OnApplicationQuit:This function is called on all game objects before the application is quit. In the editor it is called when the user stops playmode. In the web player it is called when the web view is closed.
OnApplicationQuit:在應(yīng)用退出之前所有的游戲?qū)ο蠖紩{(diào)用這個函數(shù)。在編輯器中當(dāng)用戶停止播放時它將被調(diào)用。在webplayer中,當(dāng)網(wǎng)頁關(guān)閉時被調(diào)用。
OnDisable:This function is called when the behaviour becomes disabled or inactive.
OnDisable: 當(dāng)行為不可用或非激活時,這個函數(shù)被調(diào)用。
So in conclusion, this is the execution order for any given script:
因此結(jié)論是,對于任意給定的腳本的執(zhí)行順序為:
? ? ??(當(dāng)在腳本里用代碼添加一個組件時,會直接依次調(diào)用Awake和OnEnabled(而不是當(dāng)添加組件的代碼所在方法結(jié)束后才調(diào)用它們?。。。。?/b>
All Awake calls所有Awake調(diào)用
OnEnabled/OnDisabled 被調(diào)用(當(dāng)組件被激活或不激活時被調(diào)用,在同一幀中不限制調(diào)用次數(shù))
OntriggerEnter.......等碰撞器/觸發(fā)器函數(shù)的調(diào)用(有碰撞或觸發(fā)才會調(diào)用)
All Start Calls所有Start調(diào)用
while(stepping towards variable delta time)
All FixedUpdate functions所有FixedUpdate函數(shù)
Physics simulation物理模擬
Rigidbody interpolation applies transform.position and rotation
剛體插值應(yīng)用于transform.position 和 rotation
OnMouseDown/OnMouseUp etc. eventsOnMouseDown/OnMouseUp等事件
All Update functions所有Update函數(shù)
Animations are advanced, blended and applied to transform高級動畫、混合并應(yīng)用到變換
All LateUpdate functions所有LateUpdate函數(shù)
Rendering渲染
Hints 提示
If you start a coroutine in LateUpdate it will also be called after LateUpdate just before rendering.
如果你在LateUpdate中開啟一個協(xié)程,它將在LateUpdate之后渲染之前也會被調(diào)用。
Coroutines are executed after all Update functions.
協(xié)程在所有的Update函數(shù)完成后執(zhí)行。
