前言
我们可能经常会在一些资料中看到何时应该运用何种Upate函数,例如:
- 物理运算应该放在FixedUpdate中
- Input检测应该放在Update中
- 相机应该放在LateUpate中
但是却不知道所以然。本文将会以几个案例来分析上面的新手指令清单的更深一层的原因。
定义
首先,我们先从官网简单看下不同的定义,后面再通过案例详细说明。
Update
Update is called every frame, if the MonoBehaviour is enabled.
Update is the most commonly used function to implement any kind of game script. Not every MonoBehaviour script needs Update.
上面重点说的是Update每帧都会被调用。
FixedUpdate
Frame-rate independent MonoBehaviour.FixedUpdate message for physics calculations.
首先FixedUpdate和帧率是不相关的。
MonoBehaviour.FixedUpdate has the frequency of the physics system; it is called every fixed frame-rate frame. Compute Physics system calculations after FixedUpdate. 0.02 seconds (50 calls per second) is the default time between calls. Use Time.fixedDeltaTime to access this value. Alter it by setting it to your preferred value within a script, or, navigate to Edit > Settings > Time > Fixed Timestep and set it there. The FixedUpdate frequency is more or less than Update. If the application runs at 25 frames per second (fps), Unity calls it approximately