简单计时器的实现方法(类似秒表)
void Start () {
}
float timer = 0 ;
int second = 0 ;
int minute = 0 ;
int hour = 0 ;
string timeStr = "00:00:00" ;
void Update () {
timer += Time.deltaTime;
if (timer>=1 )
{
second++;
timer -= 1 ;
}
if (second>=60 )
{
minute++;
second = 0 ;
}
if (minute>=60 )
{
hour++;
minute = 0 ;
}
if (hour>=24 )
{
hour = 0 ;
}
timeStr = string .Format("{0:00}:{1:00}:{2:00}" , hour, minute, second);
}
string buttonStr = "暂停" ;
void OnGUI()
{
string str = buttonStr;
GUI.Label(new Rect(100 , 100 , 100 , 60 ), timeStr);
if (GUI.Button(new Rect(0 ,0 ,100 ,100 ),"1倍速度" ))
{
Time.timeScale = 1 ;
}
if (GUI.Button(new Rect(0 ,105 ,100 ,100 ),"两倍速度" ))
{
Time.timeScale = 2 ;
}
if (GUI.Button(new Rect(0 , 210 , 100 , 100 ), str))
{
if (str=="暂停" )
{
Time.timeScale = 0 ;
buttonStr = "开始" ;
}
if (str=="开始" )
{
Time.timeScale = 1 ;
buttonStr = "暂停" ;
}
}
}