private IEnumerator StartContinuousCoroutine(float seconds, TimerElapsedCallback t) { Debug.Log("StartContinousCoroutine"); long timeElapsed = 0; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); while (true) { sw.Start(); yield return(new WaitForSeconds(seconds)); sw.Stop(); timeElapsed += sw.ElapsedMilliseconds; t?.Invoke(timeElapsed); } }
//the coroutine thats actually run private IEnumerator StartCountdownCoroutine(float startValue, float waitForDelay, float countDownBy, TimerElapsedCallback t, TimerFinished tf) { Debug.Log("StartCountdownCoroutine"); //stopwatch to track time elapsed System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); while (startValue > 0) { sw.Start(); yield return(new WaitForSeconds(waitForDelay)); //update 'time-remaining' startValue -= countDownBy; sw.Stop(); //callback for interval t?.Invoke(sw.ElapsedMilliseconds); } //callback for end of timer tf?.Invoke(); Debug.Log("Timer finished " + sw.ElapsedMilliseconds); }
//start timer with full customisable public void StartTimer(float startValue, float waitForDelay, float countDownBy, TimerElapsedCallback t, TimerFinished tf) { Debug.Log("StartTimer2"); coroutine = StartCoroutine(StartCountdownCoroutine(startValue, waitForDelay, countDownBy, t, tf)); }
//start timer for seconds with default parameters public void StartTimer(float delayInSeconds, TimerElapsedCallback t, TimerFinished tf) { Debug.Log("StartTimer"); coroutine = StartCoroutine(StartCountdownCoroutine(delayInSeconds, delayInSeconds, delayInSeconds, t, tf)); }
public void StartTimer(float seconds, TimerElapsedCallback t) { Debug.Log("StartContinousTimer"); coroutine = StartCoroutine(StartContinuousCoroutine(seconds, t)); }