Unity倒计时
1 public Text CountDownTxt; 2 DateTime endTime = DateTime.Parse("2020-10-24"); 3 public float timer = 1f; //刷新频率 4 float time = 0; 5 double totalSeconds = 0; 6 private void Update() 7 { 8 if (time<timer) 9 { 10 time += Time.unscaledDeltaTime; 11 } 12 else 13 { 14 time = 0; 15 Countdown(); 16 } 17 } 18 void Countdown() 19 { 20 totalSeconds = (endTime - DateTime.Now).TotalSeconds; 21 CountDownTxt.text = (totalSeconds / 3600).ToString("D2") + ":" + (totalSeconds % 3600 / 60).ToString("D2") + ":" + (totalSeconds % 60).ToString("D2"); 22 }