private string getTimerString(TimerHandle th) { string pattern = "<size=13>Status:<color={0}>{1}</color>\tTag:{2} </size>"; return string.Format(pattern, th.bPaused? "red" : "green", th.bPaused? "Paused":"Running", th.tag ); }
public static TimerHandle AddTimer(this MonoBehaviour mb, TimerFunc func, float delay, float interval, int maxCount, string tag = "defaultTag") { TimerHandle th = new TimerHandle(); th.func = func; th.delay = delay; th.interval = interval; th.maxCount = maxCount; th.tag = tag; mb.StartCoroutine(_timer(th)); return th; }
/// <summary> /// Adds the timer to GLOBAL /// </summary> /// <returns>The timer handle.</returns> /// <param name="func">Func.</param> /// <param name="delay">Delay.</param> /// <param name="interval">Interval.</param> /// <param name="maxCount">Max count.</param> /// <param name="tag">Tag, only for debug.</param> public static TimerHandle addTimerGlobal(TimerFunc func, float delay, float interval, int maxCount, string tag = "defaultTag") { if (_Instance == null) { GameObject go = new GameObject("Timer"); DontDestroyOnLoad(go); _Instance = go.AddComponent <Timer>(); _Instance.timerName = "Global"; } TimerHandle th = _Instance.addTimerInternal(func, delay, interval, maxCount, tag); return(th); }
private IEnumerator _timer(TimerHandle th) { int countNow = 0; if (th.delay > 0f) { yield return(new WaitForSeconds(th.delay)); } while (th.maxCount != 0) { if (th.bDead == true) { break; } if (bPaused == true || th.bPaused == true) { yield return(0); } else { if (th.maxCount != INFINITE) { ++countNow; } #if UNITY_EDITOR ++th.countNow; #endif if (th.func()) //If TimerFunc return true then end Timer Immediately. { break; } if (th.maxCount != INFINITE && countNow >= th.maxCount) { break; } yield return(new WaitForSeconds(th.interval)); } } //clean up timer th.bDead = true; if (th.completeCB != null) { th.completeCB(); } #if UNITY_EDITOR timerHandleList.Remove(th); #endif }
public TimerHandle addTimerInternal(TimerFunc func, float delay, float interval, int maxCount, string tag = "defaultTag") { TimerHandle th = new TimerHandle(); th.func = func; th.delay = delay; th.interval = interval; th.maxCount = maxCount; th.tag = tag; StartCoroutine(_timer(th)); #if UNITY_EDITOR th.countNow = 0; timerHandleList.AddLast(th); #endif return(th); }
private static IEnumerator _timer(TimerHandle th) { int countNow = 0; if(th.delay > 0f) yield return new WaitForSeconds(th.delay); while(th.maxCount != 0) { if(th.bDead == true) break; if(th.bPaused == true) { yield return 0; } else { if(th.maxCount != Timer.INFINITE) { ++countNow; } #if UNITY_EDITOR ++th.countNow; #endif if(th.func()) //If TimerFunc return true then end Timer Immediately. break; if(th.maxCount != Timer.INFINITE && countNow >= th.maxCount) break; yield return new WaitForSeconds(th.interval); } } //clean up timer th.bDead = true; if(th.completeCB != null) { th.completeCB(); } }
public TimerHandle addTimerInternal(TimerFunc func, float delay, float interval, int maxCount, string tag = "defaultTag") { TimerHandle th = new TimerHandle(); th.func = func; th.delay = delay; th.interval = interval; th.maxCount = maxCount; th.tag = tag; StartCoroutine(_timer(th)); #if UNITY_EDITOR th.countNow = 0; timerHandleList.AddLast(th); #endif return th; }