Graphics Reference
In-Depth Information
We reset lastTime to the current time taken from Time.realtimeSinceStartup, which effec-
tively removes the time counted between now and the last update and starts counting from
now instead. Finally, we make a quick call to UpdateTimer() to start the timer process again.
Whenever we need to display the time on the screen, it is most likely that we will need
it to be formatted in an established format such as minutes:seconds:milliseconds. To do
this, a little work is required to calculate the required units from our currentTime vari-
able, as currentTime is simply a float value containing a number that bears no resemblance
to what we need. Time.realtimeSinceStartup returns time as reported by a system timer in
seconds. The GetFormattedTime function takes this value and breaks it up into the units
we need, then puts together a nicely formatted string and returns it.
public string GetFormattedTime ()
{
// carry out an update to the timer so it is 'up to date'
UpdateTimer();
Note that when GetFormattedTime() is called, we first make an UpdateTimer call to
update the currentTime value. As mentioned earlier, the timer does not update itself—it is
a lazy updater in that it only updates when we ask it to do something. In this case, we ask
it for a formatted time string.
From there on, we are simply doing the math to get the minutes, seconds and mil-
liseconds values from currentTime:
// grab minutes
aMinute=(int)currentTime/60;
aMinute=aMinute%60;
// grab seconds
aSecond=(int)currentTime%60;
// grab milliseconds
aMillis=(int)(currentTime*100)%100;
After minutes, seconds, and milliseconds values have been calculated and stored into
the integer variables aMinute, aSecond, and aMillis, three new strings called seconds,
minutes, and mills are built from them:
// format strings for individual mm/ss/mills
tmp=(int)aSecond;
seconds=tmp.ToString();
if(seconds.Length<2)
seconds="0"+seconds;
tmp=(int)aMinute;
minutes=tmp.ToString();
if(minutes.Length<2)
minutes="0"+minutes;
tmp=(int)aHour;
hour=tmp.ToString();
if(hour.Length<2)
hour="0"+hour;
Search WWH ::




Custom Search