using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Timer : MonoBehaviour { public bool startCount = false; public static Timer Instance; void Awake() { Instance = this; } void Start() {} void Update() { if (startCount) { Timer1(); //倒计时 Timer2();//正计时 } } string s; string m; private float totalTime = 0; public float second = 0; public float min = 10; public Text txtTimer; private void Timer1() { totalTime += Time.deltaTime; if (totalTime >= 1) { second--; if (second < 0) { second = 59; min--; } totalTime = 0; } s = second.ToString(); m = min.ToString(); if (second < 10) { s = string.Format("0{0}", second); } if (min < 10) { m = string.Format("0{0}", min); } txtTimer.text = string.Format("{0}:{1}", m, s); if (min < 0) { Debug.Log("时间结束了0-0"); } } private float totalTime2 = 0; public float second2 = 0; public float min2 = 0; string s2; string m2; public string timeAllstr2; private void Timer2() { totalTime2 += Time.deltaTime; if (totalTime2 >= 1) { second2++; if (second2 > 59) { second2 = 0; min2++; Debug.Log("x过来一秒xx"); } totalTime2 = 0; } s2 = second2.ToString(); m2 = min2.ToString(); if (min2 > 59) { min2 = 0; second2 = 0; } if (second2 < 10) { s2 = string.Format("0{0}", second2); } if (min2 < 10) { m2 = string.Format("0{0}", min2); } timeAllstr2 = string.Format("{0}分{1}秒", m2, s2); } public void StartCount() { startCount = true; } public void StopCount() { startCount = false; } }