Example #1
0
    //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);
    }
Example #2
0
    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);
        }
    }