1. 程式人生 > >Unity3D事件函數的執行順序

Unity3D事件函數的執行順序

攝像機 觸發 var med lac 視圖 專業 chains war

In Unity scripting, there are a number of event functions that get executed in a predetermined order as a script executes. This execution order is described below:

在Unity腳本中,有一些按照預定順序執行的事件函數,腳本即是按照此順序執行的。這個執行順序描述如下:

First Scene Load 第一個場景加載

These functions get called when a scene starts (once for each object in the scene).

這些函數在場景開始時就被調用了(對場景中的每個對象執行一次)。

  • Awake: This function is always called before any Start functions and also just after a prefab is instantiated.
    Awake:這個函數是在Start函數之前以及預制物體實例化後被調用。
  • OnEnable: (only called if the Object is active): This function is called just after the object is enabled.
    OnEnable:(僅在對象激活狀態下可用):這個函數在對象可用之後被調用。

Before the first frame update 第一幀更新之前

  • Start: Start is called before the first frame update only if the script instance is enabled.
    Start:如果腳本實例化被啟用,則Start函數在第一幀更新之前被調用。

In between frames 執行期間

  • 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:若暫停被檢測到,當前幀執行後就調用此函數,在正常的運行期間調用是有效的。在OnApplicationPause被調用後,額外的用一幀來顯示圖像表明暫停狀態。

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.

這裏有一些不同的事件函數供你來追蹤遊戲邏輯,動畫和相機的位置等。在Update()函數中執行大多數任務是比較常見的,然而也有其他函數可以使用。

  • 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()函數調用的更頻繁。當幀率比較低時,它每幀被調用多次,如果幀率比較高,它有可能就不會被調用。所有的物理計算和更新都立即發生在FixedUpdate()之後。當在FixedUpdate()中計算物體移動時,你不需要乘以Time.deltaTime。因為FixedUpdate()是基於可靠的定時器的,不受幀率的影響。
  • Update: Update() is called once per frame. It is the main workhorse function for frame updates.
    Update() 每幀調用一次。這是幀更新的主要函數。
  • 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()執行後,LateUpdate() 也是每幀都被調用。在Update()中執行的任何計算都會在LateUpdate()開始之前完成。LateUpdate()的一個常見應用就是第三人稱控制器的相機跟隨。如果你把角色的移動和旋轉放在Update()中,那麽你就可以把所有相機的移動旋轉放在LateUpdate()。這是為了在相機追蹤角色位置之前,確保角色已經完成移動。

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:在相機剔除場景前被調用。剔除取決於物體在相機中是否可見。OnPreCull僅在剔除執行之前被調用。
  • OnBecameVisible/OnBecameInvisible: Called when an object becomes visible/invisible to any camera.
    OnBecameVisible/OnBecameInvisible:當物體在任何相機中可見/不可見時被調用。
  • OnWillRenderObject: Called once for each camera if the object is visible.
    OnWillRenderObject:如果物體可見,它將為每個攝像機調用一次。
  • OnPreRender: Called before the camera starts rendering the scene.
    OnPreRender: 在相機渲染場景之前被調用。
  • 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:在所有固定場景渲染之後被調用。此時你可以使用GL類或者Graphics.DrawMeshNow來畫自定義的幾何體。
  • OnPostRender: Called after a camera finishes rendering the scene.
    OnPostRender: 在相機完成場景的渲染後被調用。
  • OnRenderImage(Pro only): Called after scene rendering is complete to allow postprocessing of the screen image.
    OnRenderImage(僅專業版):在場景渲染完成後被調用,用來對屏幕的圖像進行後處理。
  • 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: 每幀被調用多次用來回應GUI事件。布局和重繪事件先被執行,接下來是為每一次的輸入事件執行布局和鍵盤/鼠標事件。
  • OnDrawGizmos Used for drawing Gizmos in the scene view for visualisation purposes.
    OnDrawGizmos:為了可視化的目的在場景視圖中繪制小圖標。

Coroutine 協同程序(協程)

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:

正常情況下協程是在Update函數返回時執行。協程的功能是,延緩其執行(yield) ,直到給定的YieldInstruction完成。協程的不同用途:

  • yield; The coroutine will continue after all Update functions have been called on the next frame.
    yield:協程在所有的Update函數於下一幀調用後繼續執行。
  • yield WaitForSeconds(2); Continue after a specified time delay, after all Update functions have been called for the frame
    yield WaitForSeconds(2):在一個指定的時間延遲後繼續執行,在所有的Update函數被調用之後。
  • yield WaitForFixedUpdate(); Continue after all FixedUpdate has been called on all scripts
    yield WaitForFixedUpdate():在所有腳本上所有的FixedUpdate被調用之後繼續執行。
  • yield WWW Continue after a WWW download has completed.
    yield WWW:在WWW加載完成之後繼續執行。
  • yield StartCoroutine(MyFunc); Chains the coroutine, and will wait for the MyFunc coroutine to complete first.
    yield StartCoroutine(MyFunc):用於鏈接協程,此將等待MyFunc協程完成先。

When the Object is Destroyed 當對象被銷毀時

  • 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: 這個函數在所有幀更新之後被調用,在對象存在的最後一幀(對象將銷毀來響應Object.Destroy或關閉一個場景)。

When Quitting 當退出時

These functions get called on all the active objects in your scene, :

這些函數對於場景中的所有激活狀態的物體都會被調用。

  • 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:在應用退出之前所有的遊戲對象都會調用這個函數。在編輯器中當用戶停止播放時它將被調用。在webplayer中,當網頁關閉時被調用。
  • OnDisable: This function is called when the behaviour becomes disabled or inactive.
    OnDisable: 當行為不可用或非激活時,這個函數被調用。

So in conclusion, this is the execution order for any given script:

因此結論是,對於任意給定的腳本的執行順序為:

  1. All Awake calls 所有Awake調用
  2. All Start Calls 所有Start調用
  3. while (stepping towards variable delta time)
    1. All FixedUpdate functions 所有FixedUpdate函數
    2. Physics simulation 物理模擬
    3. OnEnter/Exit/Stay trigger functions OnEnter/Exit/Stay觸發函數
    4. OnEnter/Exit/Stay collision functions OnEnter/Exit/Stay碰撞函數
  4. Rigidbody interpolation applies transform.position and rotation
    剛體插值應用於transform.position 和 rotation
  5. OnMouseDown/OnMouseUp etc. events OnMouseDown/OnMouseUp等事件
  6. All Update functions 所有Update函數
  7. Animations are advanced, blended and applied to transform 高級動畫、混合並應用到變換
  8. All LateUpdate functions 所有LateUpdate函數
  9. Rendering 渲染

Hints 提示

    • If you start a coroutine in LateUpdate it will also be called after LateUpdate just before rendering.
      如果你在LateUpdate中開啟一個協程,它將在LateUpdate之後渲染之前也會被調用。
    • Coroutines are executed after all Update functions.
      協程在所有的Update函數完成後執行。

Unity3D事件函數的執行順序