Await.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Threading;
  4. namespace UnityAsync
  5. {
  6. public static partial class Await
  7. {
  8. static readonly AwaitInstructionAwaiter<WaitForFrames> nextUpdate = new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(1));
  9. static readonly AwaitInstructionAwaiter<WaitForFrames> nextLateUpdate = new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(1), FrameScheduler.LateUpdate);
  10. static readonly AwaitInstructionAwaiter<WaitForFrames> nextFixedUpdate = new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(1), FrameScheduler.FixedUpdate);
  11. /// <summary>
  12. /// Quick access to Unity's <see cref="System.Threading.SynchronizationContext"/>.
  13. /// </summary>
  14. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  15. public static SynchronizationContext UnitySyncContext() => AsyncManager.UnitySyncContext;
  16. /// <summary>
  17. /// Quick access to the background <see cref="System.Threading.SynchronizationContext"/>.
  18. /// </summary>
  19. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  20. public static SynchronizationContext BackgroundSyncContext() => AsyncManager.BackgroundSyncContext;
  21. /// <summary>
  22. /// Convenience function to skip a single frame, equivalent to Unity's <c>yield return null</c>.
  23. /// </summary>
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. public static AwaitInstructionAwaiter<WaitForFrames> NextUpdate() => nextUpdate;
  26. /// <summary>
  27. /// Convenience function to skip a number of frames, equivalent to multiple <c>yield return null</c>s.
  28. /// </summary>
  29. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  30. public static AwaitInstructionAwaiter<WaitForFrames> Updates(int count) => new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(count));
  31. /// <summary>
  32. /// Convenience function to skip a single frame and continue in the LateUpdate loop.
  33. /// </summary>
  34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35. public static AwaitInstructionAwaiter<WaitForFrames> NextLateUpdate() => nextLateUpdate;
  36. /// <summary>
  37. /// Convenience function to skip multiple frames and continue in the LateUpdate loop.
  38. /// </summary>
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public static AwaitInstructionAwaiter<WaitForFrames> LateUpdates(int count) => new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(count), FrameScheduler.LateUpdate);
  41. /// <summary>
  42. /// Convenience function to skip a single FixedUpdate frame and continue in the FixedUpdate loop. Equivalent to
  43. /// Unity's <see cref="UnityEngine.WaitForFixedUpdate"/>.
  44. /// </summary>
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public static AwaitInstructionAwaiter<WaitForFrames> NextFixedUpdate() => nextFixedUpdate;
  47. /// <summary>
  48. /// Convenience function to skip multiple FixedUpdate frames and continue in the FixedUpdate loop.
  49. /// </summary>
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public static AwaitInstructionAwaiter<WaitForFrames> FixedUpdates(int count) => new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(count), FrameScheduler.FixedUpdate);
  52. /// <summary>
  53. /// Convenience function to wait for a number of in-game seconds before continuing, equivalent to Unity's
  54. /// <see cref="UnityEngine.WaitForSeconds"/>.
  55. /// </summary>
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. public static WaitForSeconds Seconds(float duration) => new WaitForSeconds(duration);
  58. /// <summary>
  59. /// Convenience function to wait for a number of unscaled seconds before continuing. Equivalent to Unity's
  60. /// <see cref="UnityEngine.WaitForSecondsRealtime"/>.
  61. /// </summary>
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public static WaitForSecondsRealtime SecondsRealtime(float duration) => new WaitForSecondsRealtime(duration);
  64. /// <summary>
  65. /// Convenience function to wait for a condition to return true. Equivalent to Unity's
  66. /// <see cref="UnityEngine.WaitUntil"/>.
  67. /// </summary>
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public static WaitUntil Until(Func<bool> condition) => new WaitUntil(condition);
  70. /// <summary>
  71. /// Convenience function to wait for a condition to return false. Equivalent to Unity's
  72. /// <see cref="UnityEngine.WaitWhile"/>.
  73. /// </summary>
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. public static WaitWhile While(Func<bool> condition) => new WaitWhile(condition);
  76. /// <summary>
  77. /// Convenience function to wait for a condition to return true. Equivalent to Unity's
  78. /// <see cref="UnityEngine.WaitUntil"/> but state is passed to avoid closure.
  79. /// </summary>
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public static WaitUntil<TState> Until<TState>(TState state, Func<TState, bool> condition) => new WaitUntil<TState>(state, condition);
  82. /// <summary>
  83. /// Convenience function to wait for a condition to return false. Equivalent to Unity's
  84. /// <see cref="UnityEngine.WaitWhile"/> but state is passed to avoid closure.
  85. /// </summary>
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. public static WaitWhile<TState> While<TState>(TState state, Func<TState, bool> condition) => new WaitWhile<TState>(state, condition);
  88. }
  89. }