Showken
September 26, 2010, 8:38am
1
if I press a button,the time begins to run rather
than the Script run.how to get it ?
i don't know whether express my
qusetion correctly or not.my English
is so bad.
All right! actually,i want to make a
Timer ,when press a button,the time
begins to run
system
September 26, 2010, 9:10am
2
I've used a modified version of Sebas' answer , adding a button press to start the timer.
When you press 't', the startTime is recorded. Until that time, OnGUI() is returning early due to the ifcheck (is t still null).
Let me know if there's anything you don't understand.
private var startTime : float = 0.0;
var textTime : string;
function Update()
{
if ( Input.GetKeyDown("t") )
{
startTime = Time.time;
}
}
function OnGUI ()
{
if ( startTime == 0 )
return;
var guiTime = Time.time - startTime;
var minutes : int = guiTime / 60;
var seconds : int = guiTime % 60;
var fraction : int = (guiTime * 100) % 100;
textTime = String.Format ("{0:00}:{1:00}:{2:000}", minutes, seconds, fraction);
GUI.Label (Rect (400, 25, 100, 30), textTime);
}