- Home /
Heavy Processing Messes With Time.time
Hi there,
I begin my game by starting a coroutine:
void Start() {
StartCoroutine(ImportAssets());
}
In here, I wait for two seconds, and then save the current time:
IEnumerator ImportAssets() {
yield return new WaitForSeconds(2);
startTime = Time.time;
}
Then, in my Update function I test to see if a character needs importing, if it does I disable the script while it imports. (Ignore the first if statement for a moment.)
void Update() {
if (itemsLoaded == total) {
Helpers.Log(Time.time);
Helpers.Log(startTime);
Helpers.Log("Loaded in", Time.time - startTime);
total = 0;
enabled = false;
}
if (readyCharacters.Count > 0) {
enabled = false;
StartCoroutine(ImportCharacterObjects(readyCharacters[0]));
}
}
Importing the characters takes a lot of work, and actually holds a few frames for a couple of seconds. Once all the characters are imported, that if statement runs fine, however Time.time is definitely incorrect.
startTime logs out at ~2.002099 (Which is correct)
Time.time logs out at ~4.877523 (Which is incorrect, using a stop watch I measure at least 8 seconds)
As such, Loaded in logs out at ~2.875424, which is obviously also incorrect.
I've had the thought that maybe locking up frames in the game throws out its time counter? Otherwise I'd love to find out what's going wrong, as I need an accurate timer for this importing process.
Thanks in advance!
Answer by zach-r-d · Jun 24, 2015 at 02:20 AM
Time.time is not "real" time; it's affected by things like Time.timeScale, and whether the game is paused. It thus seems probable that it is also affected by Time.maximumDeltaTime (which exists to prevent well of despair scenarios), meaning that frames longer than the maximum delta time get added as if they took the maximum time amount rather than the real, higher amount.
For the desired functionality, try experimenting with some of the other properties available in Time, such as Time.realtimeSinceStartup or Time.unscaledTime. If those don't work, I would recommend using the System.Diagnostics.Stopwatch class.
$$anonymous$$akes sense!
I've switched to use Time.realtimeSinceStartup, however I also used a few *yield return null*s to split the loading so the game doesn't freeze anyway.
Thanks!
Your answer
Follow this Question
Related Questions
2D game performance keeps slowing tremendously when resolution is at 1080p (1920 x 1080) 1 Answer
Import Project from Asset store 6 Answers
How to create a framerate independent time delay in Update() without coroutines 1 Answer
Unity editor low frame rates and flashing while pressing keys 1 Answer
Why does my external monitor get x5 lower frame rate? 1 Answer