Unity TimeScale和 fixedDeltaTime的关系

1.timescale和fixedupdate的关系

https://docs.unity3d.com/ScriptReference/Time-timeScale.html

 

以下依timescale = 0.1为例。

首先定义概念:  (1)游戏世界gametime和现实世界realtime。

         (2)fixedDeltaTime和deltaTime的值是gameworld里的值。

        (3)但项目内游戏逻辑是realworld time的。 

        (4)update渲染帧和fixedupdate物理帧的概念。渲染帧是由自己设备决定的。timescale的值不影响update调用的次数。

          但影响deltaTime的值。deltatime为gameworld内两渲染帧之间的时间差值。在gameworld内,导致差值变小,deltatime的值变成0.1倍。

1.timescale不影响 fixedDeltaTime的值, 影响deltaTime的值

Except for realtimeSinceStartup and fixedDeltaTimetimeScale affects all the time and delta time measuring variables of the Time class.

2. timescale相当于游戏世界整体频率变了,可以认为是电影里面的播放速度

比如   正常是 realWorld的0.02s调用fixedUpdate一次   ;   timescale = 0.1  后物理 fixedDelataTime 还是0.02 但是 对应realWorld的 0.2s  fixedUpdate一次

3.fixedDeltaTime决定了游戏世界 fixedupdate调用的频率(所以timescale =0.1后 fixedupdate调用频率变低了)。 自己的项目要不要 乘以timeScale 用于保证和realWorld频率一致,比如timescale=0.1,手动.fixedDeltaTime = .fixedDeltaTime*timescale

可以保证 fixedupdate的调用频率和realworld保持一致。

to keep the number of FixedUpdate callbacks per frame constant, you must also multiply Time.fixedDeltaTime by timeScale.

 

 

This can be used for slow motion effects or to speed up your application. When timeScale is 1.0, time passes as fast as real time. When timeScale is 0.5 time passes 2x slower than realtime.

timescale在渲染帧表现上很有用,当值为1.0时和realtime保持一致。值为0.5的相当于0.5倍速的视频播放慢于realtime

When timeScale is set to zero your application acts as if paused if all your functions are frame rate independent. Negative values are ignored.

当function依赖time参数而不是渲染帧率的时候,timescale=0可理解为pause。负值相当于0??

Note that changing the timeScale only takes effect on the following frames. How often MonoBehaviour.FixedUpdate is executed per frame depends on the timeScale. Therefore, to keep the number of FixedUpdate callbacks per frame constant, you must also multiply Time.fixedDeltaTime by timeScale. Whether this adjustment is desirable is game-specific.
FixedUpdate 在realtime的调用次数受到timescale的间接影响,因为fixedDeltaTime值没有变,但gameworldtime流逝慢了,导致fixedupdate在realtime执行次数变少。
FixedUpdate functions and suspended Coroutines with WaitForSeconds are not called when timeScale is set to zero.

FixedUpdate functions和依赖 WaitForSeconds的协程在timescale为0不调用。

 

 

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnGUI.html

 2.OnGUI和update无关

OnGUI is called for rendering and handling GUI events.

OnGUI is the only function that can implement the "Immediate Mode" GUI (IMGUI) system for rendering and handling GUI events. Your OnGUI implementation might be called several times per frame (one call per event). For more information on GUI events see the Event reference. If the MonoBehaviour's enabled property is set to false, OnGUI() will not be called.

一个update可能会调用多次ongui event。

3.deltaTime

https://docs.unity3d.com/ScriptReference/Time-deltaTime.html

When this is called from inside MonoBehaviour.FixedUpdate, it returns Time.fixedDeltaTime.  在fixedupdate内delateTime返回的是fixedeltatime的值。

deltaTime inside MonoBehaviour.OnGUI is not reliable, because Unity might call it multiple times per frame.  在ongui内deltatime不可靠

4.TimeFrameManagement 

待看:https://docs.unity3d.com/Manual/TimeFrameManagement.html

 

posted @ 2022-01-11 15:09  sun_dust_shadow  阅读(531)  评论(0编辑  收藏  举报