using System.Collections; using UnityEngine; using UnityEngine.Events; public class TimeCounterManager : MonoBehaviour { int time = 0; string time_FormatStr = "00:00:00"; Coroutine timeCounterCoroutine; public void StartTimeCounter(UnityAction callBack) { if (timeCounterCoroutine == null) { timeCounterCoroutine = StartCoroutine(TimeCounter(callBack)); } } public void StopTimeCounter() { if (timeCounterCoroutine != null) { StopCoroutine(timeCounterCoroutine); timeCounterCoroutine = null; } } public void ClearTimeCounter() { time = 0; time_FormatStr = "00:00:00"; } public string GetTime() { return time_FormatStr; } IEnumerator TimeCounter(UnityAction callBack) { while (true) { yield return new WaitForSeconds(1f); time++; time_FormatStr = string.Format("{0:d2}:{1:d2}:{2:d2}", time / 3600, time % 3600 / 60, time % 3600 % 60); callBack?.Invoke(string.Format("{0:d2}:{1:d2}:{2:d2}", time / 3600, time % 3600 / 60, time % 3600 % 60)); } } }