C# 计时器

public class Timer : MonoBehaviour {
	[SerializeField]
	private Text _txt;
	private int _countTime = 0;

	void Start(){
		StartCoroutine(D_Time());
	}

	IEnumerator D_Time () {
		yield return new WaitForSeconds (1);
		_countTime += 1;//_countTime -= 1;
		int _hour = _countTime / 3600;
		int _minutes = (_countTime % 3600) / 60;
		int _seconds = (_countTime % 3600) % 60;
		if (_countTime < 0) {
			yield return null;
		} else{
			string bonusTime = null;
			if (_hour < 10) {
				bonusTime += "0" + (_hour==0?"0":_hour.ToString ());
			} else {
				bonusTime += _hour.ToString ();
			}
			if (_minutes < 10) {
				bonusTime += ":0" + (_minutes==0?"0":_minutes.ToString ());
			} else {
				bonusTime += ":" + _minutes.ToString ();
			}
			if (_seconds < 10) {
				bonusTime += ":0" + (_seconds==0?"0":_seconds.ToString ());
			} else {
				bonusTime += ":" + _seconds.ToString ();
			}
			_txt.text = bonusTime;
			StartCoroutine (D_Time ());//循环协同
		}
	}
}




  void Update()  
    {  
        Debug.Log("W now  " + System.DateTime.Now);        //当前时间(年月日时分秒)  
        Debug.Log("W utc  " + System.DateTime.UtcNow);     // 当前时间(年月日时分秒)  
        Debug.Log("W year  " + System.DateTime.Now.Year);  //当前时间(年)  
        Debug.Log("W month   " + System.DateTime.Now.Month); //当前时间(月)  
        Debug.Log("W day   " +  System.DateTime.Now.Day);    // 当前时间(日)  
        Debug.Log("W h    " + System.DateTime.Now.Hour);  // 当前时间(时)  
        Debug.Log("W min   " + System.DateTime.Now.Minute);  // 当前时间(分)  
        Debug.Log("W second   " + System.DateTime.Now.Second); // 当前时间(秒)  
    }  



你可能感兴趣的:(C#)