简体   繁体   English

时间在Unity Inspector中无法正确更新

[英]Time doesn't correctly update in Unity Inspector

When I run the game, Unity's inspector updates every few seconds, but totalTimeElapsed only increments in a series of 0.1 seconds. 当我运行游戏时,Unity的检查器每隔几秒钟更新一次,但是totalTimeElapsed仅在一系列的0.1秒内递增。 I tried a similar thing with totalTimeElapsed += Time.deltaTime but with the same result. 我用totalTimeElapsed += Time.deltaTime尝试了类似的事情,但结果相同。

How do I get the total time since game start? 自游戏开始以来,我如何获得总时间?

float totalTimeELapsed = 0;
int dayCount = 0;

private void Update()
{
    totalTimeElapsed = Time.time;
    dayCount += 1;

    AFunctionTakingLotsOfTime();
}

Because AFunctionTakingLotsOfTime() takes a long time, you need Time.realtimeSinceStartup 因为AFunctionTakingLotsOfTime()需要很长时间,所以您需要Time.realtimeSinceStartup

Time.realtimeSinceStartup deals in real time, rather than computed frame-rate-time amounts that are probably getting messed up due to the fact that your function takes a long time. Time.realtimeSinceStartup是实时处理的,而不是计算的帧速率时间量,由于您的函数需要很长时间,因此可能会搞砸。

Alternatively you could use DateTime . 或者,您可以使用DateTime

Note that both will not be affected by things like time scale and you may want to change your function to be less intensive, such as computing only a small chunk any given call (or by using a coroutine) or by splitting the method off onto a Job thread . 请注意,两者都不会受到时间尺度等因素的影响,并且您可能希望将函数的使用强度降低,例如在给定的调用中仅计算一小部分(或使用协程),或者将方法拆分为一个作业线程

float totalTime = 0f;

// Update is called once per frame
void Update () {

    totalTime += Time.deltaTime;
}

I believe what you are looking for would be something like this. 我相信您正在寻找的是这样的东西。 Assuming this class is present on runtime of your game, you should have an accurate variable which can tell you the elapsed time since the game started. 假设此类在您的游戏运行时出现,则您应该有一个准确的变量,该变量可以告诉您自游戏开始以来经过的时间。

You might also want to know that the update call occurs once per frame, ie. 您可能还想知道更新调用每帧发生一次,即。 if you are running at 60 FPS update will be called 60 times in that second. 如果您以60 FPS的速度运行,则在该秒内将调用60次更新。 You can try FixedUpdate which updates in real-time intervals instead. 您可以尝试使用FixedUpdate,它会实时更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM