12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- // 2014 - Pixelnest Studio
- using System;
- using System.Collections;
- using UnityEngine;
- using UniRx;
- namespace KairoEngine.Core
- {
- /// <summary>Ready to use timers for coroutines</summary>
- public class Timer
- {
- /// <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)
- {
- do
- {
- yield return new WaitForSeconds(duration);
- if (callback != null)
- callback();
- } while (repeat);
- }
- 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();
- }
- public static IEnumerator NextFrame(Action callback)
- {
- yield return new WaitForEndOfFrame();
- if (callback != null)
- callback();
- }
- public static CompositeDisposable Execute(float time, Action action)
- {
- var timer = Observable.Timer(TimeSpan.FromMilliseconds(time));
- CompositeDisposable cancel = new CompositeDisposable();
- timer.Subscribe(xs => {
- action();
- cancel.Dispose();
- }).AddTo(cancel);
- return cancel;
- }
- }
- }
|