Graphics Reference
In-Depth Information
// pull together a formatted string to return
timeString=minutes+":"+seconds+":"+mills;
return timeString;
}
public int GetTime ()
{
// remember to call UpdateTimer() before trying to use this
// function, otherwise the time value will not be up to date
return (int)(currentTime);
}
}
4.2.1 Script Breakdown
In this script, we will only update time whenever the time is requested. Although there
may be cases where the timer would need to be updated constantly, in the projects for this
topic we only need to do it on demand. So, ater the variable declarations, the TimerClass.
cs script begins with an update function called UpdateTimer(). Note that if you wanted
the timer to update constantly, you would derive the class from MonoBehavior (instead
of from ScriptableObject as it currently does) and add a call to UpdateTimer in your
main Update function.
The timer works by having a variable called currentTime that stores the amount of
time elapsed since the timer started. currentTime starts at zero, then UpdateTimer() cal-
culates how much time goes by between updates and adds it to currentTime. The value of
currentTime is then parsed into minutes, seconds, and milliseconds, and returned as a
nice, tidy formatted string in the GetFormattedTime() function of the same class.
UpdateTimer() will be the only function that updates the timer's time system:
public void UpdateTimer ()
{
// calculate the time elapsed since the last Update()
timeElapsed=Mathf.Abs(Time.realtimeSinceStartup-lastTime);
A variable called timeElapsed tracks how much time has gone by between updates.
Time.realtimeSinceStartup is used for good reason. By using realtimeSinceStartup, the
game can do whatever it likes to the Time.timeScale value (speeding up or slowing down
Unity physics updates), and realtimeSinceStartup will still provide usable values. If Time.
time were used, it would be affected by the timeScale and would fail to provide real-time
values if its value were set to anything other than 1.
// if the timer is running, we add the time elapsed to the
// current time (advancing the timer)
if(isTimerRunning)
{
currentTime+=timeElapsed*timeScaleFactor;
}
Search WWH ::




Custom Search