using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.UI; public enum TimeClass { 计时器, 倒计时 } public class TimeTool : MonoBehaviour { [Header("本地时间Text")] public Text localText; public bool localBool; [Header("倒计时或者计时器")] public Text text; public TimeClass timeClass; private int Minute, Second, CurrentTime; [Header("传进来倒计时起始秒")] public int jieTime; private bool jsqBool, djsBool; //倒计时 和计时器 void Start() { // Opentimer(); } void Update() { OpenLocalTime(); Timer(); } public void Timer() { if (djsBool) { int timer = jieTime + CurrentTime - (int)(Time.time); Minute = (timer / 60) % 60; Second = timer % 60; if (timer >= 0) { if (Minute < 10) { if (Second < 10) { text.text = "倒计时:" + "0" + Minute + ":0" + Second; } else { text.text = "倒计时:" + "0" + Minute + ":" + Second; } } else { if (Second < 10) { text.text = "倒计时:" + Minute + ":0" + Second; } else { text.text = "倒计时:" + Minute + ":" + Second; } } } else { djsBool = false; Debug.Log("倒计时结束"); } } if (jsqBool) { int timer = (int)Time.time - CurrentTime; Minute = (timer / 60) % 60; Second = timer % 60; if (Minute < 10) { if (Second < 10) { text.text = "学习时长:" + "0" + Minute + ":0" + Second; } else { text.text = "学习时长:" + "0" + Minute + ":" + Second; } } else { if (Second < 10) { text.text = "学习时长:" + Minute + ":0" + Second; } else { text.text = "学习时长:" + Minute + ":" + Second; } } } } public void Opentimer() { switch (timeClass) { case TimeClass.倒计时: CurrentTime = (int)Time.time; djsBool = true; break; case TimeClass.计时器: CurrentTime = (int)Time.time; jsqBool = true; break; } } public void OpenLocalTime() { if (localBool && localText != null) { localText.text = System.DateTime.Now.Year + "年" + System.DateTime.Now.Month + "月" + System.DateTime.Now.Day + "日" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute.ToString(); } } }