Skip to content

Instantly share code, notes, and snippets.

@valryon
Last active August 17, 2021 07:44
Show Gist options
  • Select an option

  • Save valryon/9915075 to your computer and use it in GitHub Desktop.

Select an option

Save valryon/9915075 to your computer and use it in GitHub Desktop.

Revisions

  1. Damien Mayance revised this gist Sep 10, 2015. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions TimerExample.cs
    Original file line number Diff line number Diff line change
    @@ -8,14 +8,6 @@
    //...
    }));


    // Advanced creation: you can launch it later and stop it
    //--------------------------------------------
    Timer t = new Timer(duration, false, () =>
    {
    //...
    });

    // Launch the timer
    StartCoroutine(t.Start());

  2. Damien Mayance renamed this gist Apr 26, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Damien Mayance revised this gist Apr 26, 2015. 2 changed files with 42 additions and 58 deletions.
    File renamed without changes.
    100 changes: 42 additions & 58 deletions Timer.cs
    Original file line number Diff line number Diff line change
    @@ -6,74 +6,58 @@
    /// <summary>
    /// Ready to use timers for coroutines
    /// </summary>
    public class Timer
    {
    private bool repeat;
    private bool stop;
    private float duration;
    private Action callback;

    public Timer(float duration, bool repeat, Action callback)
    {
    this.stop = false;
    this.repeat = repeat;
    this.duration = duration;
    this.callback = callback;
    }

    /// <summary>
    /// Start in co-routine
    /// <summary>
    /// Ready to use timers for coroutines
    /// </summary>
    /// <returns></returns>
    public IEnumerator Start()
    public class Timer
    {
    do
    /// <summary>
    /// Simple timer, no reference, wait and then execute something
    /// </summary>
    /// <param name="duration"></param>
    /// <param name="callback"></param>
    /// <returns></returns>
    public static IEnumerator Start(float duration, Action callback)
    {
    return Start(duration, false, callback);
    }


    /// <summary>
    /// Simple timer, no reference, wait and then execute something
    /// </summary>
    /// <param name="duration"></param>
    /// <param name="repeat"></param>
    /// <param name="callback"></param>
    /// <returns></returns>
    public static IEnumerator Start(float duration, bool repeat, Action callback)
    {
    if (stop == false)
    do
    {
    yield return new WaitForSeconds(duration);

    if (callback != null)
    callback();
    }
    } while (repeat && !stop);
    }

    /// <summary>
    /// Stop the timer (at next frame)
    /// </summary>
    public void Stop()
    {
    this.stop = true;
    }
    } while (repeat);
    }

    /// <summary>
    /// Simple timer, no reference, wait and then execute something
    /// </summary>
    /// <param name="duration"></param>
    /// <param name="callback"></param>
    /// <returns></returns>
    public static IEnumerator Start(float duration, Action callback)
    {
    return Start(duration, false, callback);
    }
    public static IEnumerator StartRealtime(float time, System.Action callback)
    {
    float start = Time.realtimeSinceStartup;
    while (Time.realtimeSinceStartup < start + time)
    {
    yield return null;
    }

    if (callback != null) callback();
    }

    /// <summary>
    /// Simple timer, no reference, wait and then execute something
    /// </summary>
    /// <param name="duration"></param>
    /// <param name="repeat"></param>
    /// <param name="callback"></param>
    /// <returns></returns>
    public static IEnumerator Start(float duration, bool repeat, Action callback)
    {
    Timer timer = new Timer(duration, repeat, callback);
    return timer.Start();
    }
    public static IEnumerator NextFrame(Action callback)
    {
    yield return new WaitForEndOfFrame();

    public bool Repeat
    {
    get { return repeat; }
    }
    }
    if (callback != null)
    callback();
    }
    }
  4. Damien Mayance created this gist Apr 1, 2014.
    24 changes: 24 additions & 0 deletions Examples.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    const float duration = 3f;

    // Simple creation: can't be stopped even if lopping
    //--------------------------------------------
    StartCoroutine(Timer.Start(duration, true, () =>
    {
    // Do something at the end of the 3 seconds (duration)
    //...
    }));


    // Advanced creation: you can launch it later and stop it
    //--------------------------------------------
    Timer t = new Timer(duration, false, () =>
    {
    //...
    });

    // Launch the timer
    StartCoroutine(t.Start());

    // Ask to stop it next frame
    t.Stop();

    79 changes: 79 additions & 0 deletions Timer.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    // 2014 - Pixelnest Studio
    using System;
    using System.Collections;
    using UnityEngine;

    /// <summary>
    /// Ready to use timers for coroutines
    /// </summary>
    public class Timer
    {
    private bool repeat;
    private bool stop;
    private float duration;
    private Action callback;

    public Timer(float duration, bool repeat, Action callback)
    {
    this.stop = false;
    this.repeat = repeat;
    this.duration = duration;
    this.callback = callback;
    }

    /// <summary>
    /// Start in co-routine
    /// </summary>
    /// <returns></returns>
    public IEnumerator Start()
    {
    do
    {
    if (stop == false)
    {
    yield return new WaitForSeconds(duration);

    if (callback != null)
    callback();
    }
    } while (repeat && !stop);
    }

    /// <summary>
    /// Stop the timer (at next frame)
    /// </summary>
    public void Stop()
    {
    this.stop = true;
    }

    /// <summary>
    /// Simple timer, no reference, wait and then execute something
    /// </summary>
    /// <param name="duration"></param>
    /// <param name="callback"></param>
    /// <returns></returns>
    public static IEnumerator Start(float duration, Action callback)
    {
    return Start(duration, false, callback);
    }


    /// <summary>
    /// Simple timer, no reference, wait and then execute something
    /// </summary>
    /// <param name="duration"></param>
    /// <param name="repeat"></param>
    /// <param name="callback"></param>
    /// <returns></returns>
    public static IEnumerator Start(float duration, bool repeat, Action callback)
    {
    Timer timer = new Timer(duration, repeat, callback);
    return timer.Start();
    }

    public bool Repeat
    {
    get { return repeat; }
    }
    }