CoroutineAsyncBridge.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #if (NET_4_6 || NET_STANDARD_2_0)
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using UnityEngine;
  10. namespace UniRx
  11. {
  12. public class CoroutineAsyncBridge : INotifyCompletion
  13. {
  14. Action continuation;
  15. public bool IsCompleted { get; private set; }
  16. CoroutineAsyncBridge()
  17. {
  18. IsCompleted = false;
  19. }
  20. public static CoroutineAsyncBridge Start<T>(T awaitTarget)
  21. {
  22. var bridge = new CoroutineAsyncBridge();
  23. MainThreadDispatcher.StartCoroutine(bridge.Run(awaitTarget));
  24. return bridge;
  25. }
  26. IEnumerator Run<T>(T target)
  27. {
  28. yield return target;
  29. IsCompleted = true;
  30. continuation();
  31. }
  32. public void OnCompleted(Action continuation)
  33. {
  34. this.continuation = continuation;
  35. }
  36. public void GetResult()
  37. {
  38. if (!IsCompleted) throw new InvalidOperationException("coroutine not yet completed");
  39. }
  40. }
  41. public class CoroutineAsyncBridge<T> : INotifyCompletion
  42. {
  43. readonly T result;
  44. Action continuation;
  45. public bool IsCompleted { get; private set; }
  46. CoroutineAsyncBridge(T result)
  47. {
  48. IsCompleted = false;
  49. this.result = result;
  50. }
  51. public static CoroutineAsyncBridge<T> Start(T awaitTarget)
  52. {
  53. var bridge = new CoroutineAsyncBridge<T>(awaitTarget);
  54. MainThreadDispatcher.StartCoroutine(bridge.Run(awaitTarget));
  55. return bridge;
  56. }
  57. IEnumerator Run(T target)
  58. {
  59. yield return target;
  60. IsCompleted = true;
  61. continuation();
  62. }
  63. public void OnCompleted(Action continuation)
  64. {
  65. this.continuation = continuation;
  66. }
  67. public T GetResult()
  68. {
  69. if (!IsCompleted) throw new InvalidOperationException("coroutine not yet completed");
  70. return result;
  71. }
  72. }
  73. public static class CoroutineAsyncExtensions
  74. {
  75. public static CoroutineAsyncBridge GetAwaiter(this Coroutine coroutine)
  76. {
  77. return CoroutineAsyncBridge.Start(coroutine);
  78. }
  79. #if !(CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6)))
  80. // should use UniRx.Async in C# 7.0
  81. #if UNITY_2018_3_OR_NEWER
  82. #pragma warning disable CS0618
  83. #endif
  84. public static CoroutineAsyncBridge<WWW> GetAwaiter(this WWW www)
  85. {
  86. return CoroutineAsyncBridge<WWW>.Start(www);
  87. }
  88. #if UNITY_2018_3_OR_NEWER
  89. #pragma warning restore CS0618
  90. #endif
  91. public static CoroutineAsyncBridge<AsyncOperation> GetAwaiter(this AsyncOperation asyncOperation)
  92. {
  93. return CoroutineAsyncBridge<AsyncOperation>.Start(asyncOperation);
  94. }
  95. public static CoroutineAsyncBridge GetAwaiter(this IEnumerator coroutine)
  96. {
  97. return CoroutineAsyncBridge.Start(coroutine);
  98. }
  99. #endif
  100. }
  101. }
  102. #endif