【发布时间】:2021-11-12 19:24:32
【问题描述】:
我最近在思考如何以更好的方式编写代码。目前,我正在做数学calculations 并想知道是否应该将Time.deltaTime 缓存到一个自己的变量中以便同步。
float a = speed * Time.deltaTime;
float b = speed / Time.deltaTime;
float c = a - b;
或者
float deltaTime = Time.deltaTime;
float a = speed * deltaTime;
float b = speed / deltaTime;
float c = a - b;
这只是我编的随机代码,但重点是调用Time.deltaTime时调用的是内部更新。
//
// Summary:
// The interval in seconds from the last frame to the current one (Read Only).
public static float deltaTime
{
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
意思是不同步,对吧?显然这是一件小事,但您仍然应该缓存 Time.deltaTime 以保持同步吗?
【问题讨论】:
-
简单地说,你不会看到任何区别。好处是写起来更短,如果你在很多地方使用它,并且你决定用另一个值替换 Time.deltaTime,比如 Time.fixedDeltaTime,那么你就有一个地方可以改变,它会影响所有其他地方。在性能方面,您将节省一打滴答声,这基本上不算什么。
-
Meaning not in sync, right?, 错误.. 在整个帧中它将具有完全相同的值。
标签: c# unity3d synchronization execution