Timer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // 2014 - Pixelnest Studio
  2. using System;
  3. using System.Collections;
  4. using UnityEngine;
  5. using UniRx;
  6. namespace KairoEngine.Core
  7. {
  8. /// <summary>Ready to use timers for coroutines</summary>
  9. public class Timer
  10. {
  11. /// <summary> Simple timer, no reference, wait and then execute something</summary>
  12. /// <param name="duration"></param>
  13. /// <param name="callback"></param>
  14. /// <returns></returns>
  15. public static IEnumerator Start(float duration, Action callback)
  16. {
  17. return Start(duration, false, callback);
  18. }
  19. /// <summary>Simple timer, no reference, wait and then execute something</summary>
  20. /// <param name="duration"></param>
  21. /// <param name="repeat"></param>
  22. /// <param name="callback"></param>
  23. /// <returns></returns>
  24. public static IEnumerator Start(float duration, bool repeat, Action callback)
  25. {
  26. do
  27. {
  28. yield return new WaitForSeconds(duration);
  29. if (callback != null)
  30. callback();
  31. } while (repeat);
  32. }
  33. public static IEnumerator StartRealtime(float time, System.Action callback)
  34. {
  35. float start = Time.realtimeSinceStartup;
  36. while (Time.realtimeSinceStartup < start + time)
  37. {
  38. yield return null;
  39. }
  40. if (callback != null) callback();
  41. }
  42. public static IEnumerator NextFrame(Action callback)
  43. {
  44. yield return new WaitForEndOfFrame();
  45. if (callback != null)
  46. callback();
  47. }
  48. /// <summary>
  49. /// Executes a function after a certain time has passed, based on Time.scale.
  50. /// </summary>
  51. /// <param name="time">In miliseconds</param>
  52. /// <param name="action">A function to be executed</param>
  53. /// <returns>A CompositeDisposable which can be used to cancel the timer</returns>
  54. public static CompositeDisposable Execute(float time, Action action)
  55. {
  56. var timer = Observable.Timer(TimeSpan.FromMilliseconds(time));
  57. CompositeDisposable cancel = new CompositeDisposable();
  58. timer.Subscribe(xs => {
  59. action();
  60. cancel.Dispose();
  61. }).AddTo(cancel);
  62. return cancel;
  63. }
  64. /// <summary>
  65. /// Executes a function after a certain real time has passed.
  66. /// </summary>
  67. /// <param name="time">In miliseconds</param>
  68. /// <param name="action">A function to be executed</param>
  69. /// <returns>A CompositeDisposable which can be used to cancel the timer</returns>
  70. public static CompositeDisposable ExecuteRealTime(float time, Action action)
  71. {
  72. var timer = Observable.Timer(TimeSpan.FromMilliseconds(time), Scheduler.MainThreadIgnoreTimeScale);
  73. CompositeDisposable cancel = new CompositeDisposable();
  74. timer.Subscribe(xs => {
  75. action();
  76. cancel.Dispose();
  77. }).AddTo(cancel);
  78. return cancel;
  79. }
  80. /// <summary>
  81. /// Executes a function after a certain real time has passed.
  82. /// </summary>
  83. /// <param name="time">In miliseconds</param>
  84. /// <param name="action">A function to be executed</param>
  85. /// <returns>A CompositeDisposable which can be used to cancel the timer</returns>
  86. public static void ExecuteRealTimeNotDisposable(float time, Action action)
  87. {
  88. var timer = Observable.Timer(TimeSpan.FromMilliseconds(time), Scheduler.MainThreadIgnoreTimeScale);
  89. CompositeDisposable cancel = new CompositeDisposable();
  90. timer.Subscribe(xs => {
  91. action();
  92. cancel.Dispose();
  93. }).AddTo(cancel);
  94. }
  95. }
  96. }