using UnityEngine; using System.Collections; using System; public class TimeRecovery : MonoBehaviour { /// /// /// private bool onCountDown = false; /// /// /// private string countDownTitle = "Start"; /// /// /// public int totalSeconds = 60; /// /// /// private int _leftSeconds=0; /// /// /// private Action callBack; /// /// /// public int leaveSeconds { get { return _leftSeconds; } set { if (_leftSeconds != value) { _leftSeconds = value; if (callBack != null) { callBack(_leftSeconds); } if (leaveSeconds <= 0) { StopTimer(); } } } } /// /// /// /// IEnumerator DoCountDown() { while (leaveSeconds > 0) { yield return new WaitForSeconds(1f); leaveSeconds--; } } /// /// /// /// public void StartTimer(int totalTs, Action leftTsChangeCallBack) { StopTimer(); totalSeconds = totalTs; leaveSeconds = totalTs; callBack = leftTsChangeCallBack; onCountDown = true; StartCoroutine(DoCountDown()); } /// /// /// public void StopTimer() { onCountDown = false; StopAllCoroutines(); } }