vs_Community.exe --layout "F:\linson\vs2017 comm\offline" --lang zh-CN
?
學習unity3d,感覺事件順序很重要。就翻譯一下官方文檔吧。
Execution Order of Event Functions
事件函數的執行順序
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 腳本中,有大量的事件按照預定的順序作為腳本來執行。
Editor
- Reset:?Reset is called to initialize the script’s properties when it is first attached to the object and also when the?Reset?command is used.
- 當第一次附加到對象或執行Reset命令時,Reset事件會被調用,來初始化腳本的屬性?(成員變量)
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. (If a GameObject is inactive during start up Awake is not called until it is made active)
- Awake函數在所有的Start函數前,且在Prefab 初始化之后執行(如果GameObject 沒有激活,那么對于此gameobject,awake事件階段,就不會執行此gameobject的awake函數)
- OnEnable:?(only called if the Object is active): This function is called just after the object is enabled. This happens when a MonoBehaviour instance is created, such as when a level is loaded or a GameObject with the script component is instantiated.
- 在gameobject是active狀態下,當gameobject被實例化的時候,會先初始化gameobject 中的特殊compenent:monobehaviour, 而在這些之后,會執行腳本的onenable 事件。
- OnLevelWasLoaded:?This function is executed to inform the game that a new level has been loaded.
- ?
Note that for objects added to the scene, the Awake and OnEnable functions for?all?scripts will be called before Start, Update, etc are called for any of them. Naturally, this cannot be enforced when an object is instantiated during gameplay.
對于scene中的所有object,腳本中的awake和onenable函數會在start,update等等函數之前調用,基本上,在對象初始化期間這是不可強制執行的。
Before the first frame update
- Start:?Start is called before the first frame update only if the script instance is enabled.
For objects added to the scene, the Start function will be called on all scripts before Update, etc are called for any of them. Naturally, this cannot be enforced when an object is instantiated during gameplay.
start在第一幀更新之前調用。當然腳本必須是enable。因為是在第一幀之前調用,所以也就是說只會執行一次。
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.
- ?
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會獨立于幀率,并依靠一個可靠的定時器來調用。
(以下猜想:所以一般會把某些物理特性,交互等修改的時機放入到fixupdate中,因為之后引擎會固定調用物理和交互的更新,所以避免目標機顯卡性能差,導致幀率過低,那么恰當的方式就是把物理特性和交互放入到fixupdate中,這樣雖然顯示慢,但整個游戲的內部邏輯運作是正常的)
?
?
- Update:?Update?is called once per frame. It is the main workhorse function for frame updates.
- LateUpdate:?LateUpdate?is called once per frame, after?Update?has finished. Any calculations that are performed in?Update?will have completed when?LateUpdatebegins. 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.
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.
- OnBecameVisible/OnBecameInvisible:?Called when an object becomes visible/invisible to any camera.
- OnWillRenderObject:?Called?once?for each camera if the object is visible.
- OnPreRender:?Called before the camera starts rendering the scene.
- OnRenderObject:?Called after all regular scene rendering is done. You can use?GL?class or?Graphics.DrawMeshNow?to draw custom geometry at this point.
- OnPostRender:?Called after a camera finishes rendering the scene.
- OnRenderImage:?Called after scene rendering is complete to allow post-processing of the image, see?Post-processing Effects.
- 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.
- OnDrawGizmos?Used for drawing Gizmos in the scene view for visualisation purposes.
Coroutines
Normal coroutine updates are run after the Update function returns. A coroutine is a function that can suspend its execution (yield) until the given YieldInstruction finishes. Different uses of Coroutines:
- yield?The coroutine will continue after all Update functions have been called on the next frame.
- yield WaitForSeconds?Continue after a specified time delay, after all Update functions have been called for the frame
- yield WaitForFixedUpdate?Continue after all FixedUpdate has been called on all scripts
- yield WWW?Continue after a WWW download has completed.
- yield StartCoroutine?Chains the coroutine, and will wait for the MyFunc coroutine to complete first.
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).
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.
- OnDisable:?This function is called when the behaviour becomes disabled or inactive.
Script Lifecycle Flowchart
The following diagram summarises the ordering and repetition of event functions during a script’s lifetime.