天天看點

unity 下一幀執行_Unity核心原理(2)深入了解FixedUpdate

Unity核心原理(2)深入了解FixedUpdate

官方文檔連結:Time and Framerate Management

Fixed Timestep

Unlike the main frame update, Unity’s physics system does work to a fixed timestep, which is important for the accuracy and consistency of the simulation. At the start of the physics update, Unity sets an “alarm” by adding the fixed timestep value onto the time when the last physics update ended. The physics system will then perform calculations until the alarm goes off.

和Update主幀循環不同,Unity的剛體系統通過固定的時間來驅動,固定的運算時間是保證模拟結果準确一緻的重要因素 。在剛體系統運算開始前,Unity會根據上一次剛體運算完成的時間再加上Fixed Timestep的所設定的值來作為這一次剛體運算時間的限定範圍。然後剛體系統會開始計算,直到達到這個時間限制臨界點。

You can change the size of the fixed timestep from the Time Manager and you can read it from a script using the Time.fixedDeltaTime property. Note that a lower value for the timestep will result in more frequent physics updates and more precise simulation but at the cost of greater CPU load. You probably won’t need to change the default fixed timestep unless you are placing high demands on the physics engine.

通過Time Manager可以調整Fixed Timestep的值,也可以在腳本中通過通路Time.fixedDeltaTime屬性來讀取這個值。注意,更低的Fixed Timestep值會讓剛體模拟次數更頻繁,結果也更精确,但代價是CPU的性能開銷也會更大。除非你對物體引擎有更高的要求,否則一般不建議更改預設的設定。

Maximum Allowed Timestep

The fixed timestep keeps the physical simulation accurate in real time but it can cause problems in cases where the game makes heavy use of physics and the gameplay framerate has also become low (due to a large number of objects in play, say). The main frame update processing has to be “squeezed” in between the regular physics updates and if there is a lot of processing to do then several physics updates can take place during a single frame. Since the frame time, positions of objects and other properties are frozen at the start of the frame, the graphics can get out of sync with the more frequently updated physics.

Fixed Timestep保證了剛體模拟的實時準确運算,但也會導緻一些問題,當遊戲中實體運算量比較多時,會導緻遊戲幀率變低(因為遊戲中的物體數量非常多,運算量會非常大)。而遊戲的主循環(Update)必須在在正常的實體運算FixedUpdate()調用之間進行,當有大量的物體運算要進行處理時就會在一個主循環幀(Update)當中進行多次實體運算(FixedUpdate).在主循環幀開始的時候,物體的位置和其它屬性都是固定不變的,是以在這一幀裡顯示卡所顯示的最終結果與更高頻率的實體運算結果是不同步的。(Plane備注:其實就是浪費了計算資源,因為中間的計算的結果是不會顯示的。)

Naturally, there is only so much CPU power available but Unity has an option to let you effectively slow down physics time to let the frame processing catch up. The Maximum Allowed Timestep setting (in the Time Manager) puts a limit on the amount of time Unity will spend processing physics and FixedUpdate calls during a given frame update. If a frame update takes longer than Maximum Allowed Timestep to process, the physics engine will “stop time” and let the frame processing catch up. Once the frame update has finished, the physics will resume as though no time has passed since it was stopped. The result of this is that rigidbodies will not move perfectly in real time as they usually do but will be slowed slightly. However, the physics “clock” will still track them as though they were moving normally. The slowing of physics time is usually not noticeable and is an acceptable trade-off against gameplay performance.

當然,CPU的資源是有限的,Unity有一個設定項,可以有效地降低實體運算時間,以便于讓主循環可以和追上實體運算的幀率。Time Manager中的Maximum Allowed Timestep就是給處理物體運算的FixedUpdate加上一個限制。如果某一幀的實體運算時間超過了這個值,實體引擎就會立即停止運算,以便讓主循環Update可以追上,一旦這一幀主循環Update運作完成,實體引擎就會從它暫停的地方恢複計算就像它從來沒有停止過一樣。這樣做的結果是,剛體不會像平時那樣實時完美地移動,而是會稍微放慢速度。 然而,實體運算結果的連貫性得到了保證,就像它們在正常移動一樣。通常情況下,實體運算結果變慢的程度不太明顯,這是一個可接受的性能與表現之間的平衡。