OOIII/

unity实现有暂停和重置功能的倒计时

用unity实现一个有暂停和重置功能的倒计时,代码如下,一共用到6个变量

using UnityEngine;
using System.Collections;
//倒计时120s
public class Backtime : MonoBehaviour {
	float time1;
	float time2;
	float time3=120.0f;
	float T; 
	bool flag=false;
	string s="start";
	void OnGUI(){
//点击按钮倒计时开始,再次点击暂停计时
		if (GUI.Button (new Rect (0, 0, 50, 50), s)) {
			if(!flag){
				time2=Time.time;
				s="pause";}else{
					time3=time1;
					s="start";}
			flag=!flag;
		}
		time1= time3+time2-Time.time;
		if (flag) {
			T=time1;} else {
				T=time3;	
			}
		GUI.Label (new Rect (0, 50, 100, 50), T.ToString ());
//点击按钮重置倒计时
		if (GUI.Button (new Rect (50, 0, 50, 50), "reset")) {
			time3=120.0f;
			flag=false;
		}
	}
}