- Home /
What's the best way to get the total time since the start of the current frame?
As title. basically i want to know how much time has passed since this frame started processing, while we are still in this frame.
After a quick look through the documentation it looks like using Time.realtimeSinceStartup is your best bet combined with Time.time (which is the timestamp at the start of this frame).
http://docs.unity3d.com/ScriptReference/Time-realtimeSinceStartup.html
something is very wrong here.
public static float CurrentFrameTime()
{
return Time.realtimeSinceStartup - Time.time;
}
This consistently returns a value of approximately 0.34 seconds when i'm calling it. If that were true my game would be running at a snail pace, and yet i'm running at a pretty smooth 50 fps
Answer by Kiwasi · Dec 06, 2014 at 10:24 AM
Real time could be used by storing the value of realtime at the beginning of the frame. The subtract it from realtime when you need to check it.
A StopWatch would also work. Manipulate your script execution order so it starts at the beginning of each frame. Then you can check it while you are running.
Time.realtimeSinceStartup definitely changes during a frame. It changes from one line of code to the next, depending on how long it took to execute the instructions for that code.
if that's so, any idea why my example code didn't do the trick?
In any case i'm using a stopwatch now, and it works well. but still..
@Nanako: Time.time runs completely independent from realtimeSinceStartup. Time.time is increased "manually" each frame and is affected by the timescale. realtimeSinceStartup is basically your system time ($$anonymous$$us the start offset). It can update within a frame or not at all between two or more frames depending on the underlying system timer and your framerate.
Using a stopwatch is the best approach i think. You could use a seperate script which you place at the top of your execution order to ensure it's called first. If this script you would Reset and Start your Stopwatch. In any other script you then can read the passed time.
Time.realtimeSinceStertup can be used ins$$anonymous$$d of the stopwatch, however since it's a float value the larger the value gets over time the more you lose precision. Since you're interested in sub-frame-times a Stopwatch is way better in this case.
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
SendMessage is running before Start 1 Answer
unity doesnt start after registering 2 Answers
Help from start 3 Answers
How do I start game paused 2 Answers