UnityBindingExtensions.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Threading;
  3. using UnityEngine;
  4. #if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT
  5. using UnityEngine.UI;
  6. #endif
  7. namespace Cysharp.Threading.Tasks
  8. {
  9. public static class UnityBindingExtensions
  10. {
  11. #if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT
  12. // <string> -> Text
  13. public static void BindTo(this IUniTaskAsyncEnumerable<string> source, UnityEngine.UI.Text text, bool rebindOnError = true)
  14. {
  15. BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
  16. }
  17. public static void BindTo(this IUniTaskAsyncEnumerable<string> source, UnityEngine.UI.Text text, CancellationToken cancellationToken, bool rebindOnError = true)
  18. {
  19. BindToCore(source, text, cancellationToken, rebindOnError).Forget();
  20. }
  21. static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable<string> source, UnityEngine.UI.Text text, CancellationToken cancellationToken, bool rebindOnError)
  22. {
  23. var repeat = false;
  24. BIND_AGAIN:
  25. var e = source.GetAsyncEnumerator(cancellationToken);
  26. try
  27. {
  28. while (true)
  29. {
  30. bool moveNext;
  31. try
  32. {
  33. moveNext = await e.MoveNextAsync();
  34. repeat = false;
  35. }
  36. catch (Exception ex)
  37. {
  38. if (ex is OperationCanceledException) return;
  39. if (rebindOnError && !repeat)
  40. {
  41. repeat = true;
  42. goto BIND_AGAIN;
  43. }
  44. else
  45. {
  46. throw;
  47. }
  48. }
  49. if (!moveNext) return;
  50. text.text = e.Current;
  51. }
  52. }
  53. finally
  54. {
  55. if (e != null)
  56. {
  57. await e.DisposeAsync();
  58. }
  59. }
  60. }
  61. // <T> -> Text
  62. public static void BindTo<T>(this IUniTaskAsyncEnumerable<T> source, UnityEngine.UI.Text text, bool rebindOnError = true)
  63. {
  64. BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
  65. }
  66. public static void BindTo<T>(this IUniTaskAsyncEnumerable<T> source, UnityEngine.UI.Text text, CancellationToken cancellationToken, bool rebindOnError = true)
  67. {
  68. BindToCore(source, text, cancellationToken, rebindOnError).Forget();
  69. }
  70. public static void BindTo<T>(this AsyncReactiveProperty<T> source, UnityEngine.UI.Text text, bool rebindOnError = true)
  71. {
  72. BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
  73. }
  74. static async UniTaskVoid BindToCore<T>(IUniTaskAsyncEnumerable<T> source, UnityEngine.UI.Text text, CancellationToken cancellationToken, bool rebindOnError)
  75. {
  76. var repeat = false;
  77. BIND_AGAIN:
  78. var e = source.GetAsyncEnumerator(cancellationToken);
  79. try
  80. {
  81. while (true)
  82. {
  83. bool moveNext;
  84. try
  85. {
  86. moveNext = await e.MoveNextAsync();
  87. repeat = false;
  88. }
  89. catch (Exception ex)
  90. {
  91. if (ex is OperationCanceledException) return;
  92. if (rebindOnError && !repeat)
  93. {
  94. repeat = true;
  95. goto BIND_AGAIN;
  96. }
  97. else
  98. {
  99. throw;
  100. }
  101. }
  102. if (!moveNext) return;
  103. text.text = e.Current.ToString();
  104. }
  105. }
  106. finally
  107. {
  108. if (e != null)
  109. {
  110. await e.DisposeAsync();
  111. }
  112. }
  113. }
  114. // <bool> -> Selectable
  115. public static void BindTo(this IUniTaskAsyncEnumerable<bool> source, Selectable selectable, bool rebindOnError = true)
  116. {
  117. BindToCore(source, selectable, selectable.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
  118. }
  119. public static void BindTo(this IUniTaskAsyncEnumerable<bool> source, Selectable selectable, CancellationToken cancellationToken, bool rebindOnError = true)
  120. {
  121. BindToCore(source, selectable, cancellationToken, rebindOnError).Forget();
  122. }
  123. static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable<bool> source, Selectable selectable, CancellationToken cancellationToken, bool rebindOnError)
  124. {
  125. var repeat = false;
  126. BIND_AGAIN:
  127. var e = source.GetAsyncEnumerator(cancellationToken);
  128. try
  129. {
  130. while (true)
  131. {
  132. bool moveNext;
  133. try
  134. {
  135. moveNext = await e.MoveNextAsync();
  136. repeat = false;
  137. }
  138. catch (Exception ex)
  139. {
  140. if (ex is OperationCanceledException) return;
  141. if (rebindOnError && !repeat)
  142. {
  143. repeat = true;
  144. goto BIND_AGAIN;
  145. }
  146. else
  147. {
  148. throw;
  149. }
  150. }
  151. if (!moveNext) return;
  152. selectable.interactable = e.Current;
  153. }
  154. }
  155. finally
  156. {
  157. if (e != null)
  158. {
  159. await e.DisposeAsync();
  160. }
  161. }
  162. }
  163. #endif
  164. // <T> -> Action
  165. public static void BindTo<TSource, TObject>(this IUniTaskAsyncEnumerable<TSource> source, TObject monoBehaviour, Action<TObject, TSource> bindAction, bool rebindOnError = true)
  166. where TObject : MonoBehaviour
  167. {
  168. BindToCore(source, monoBehaviour, bindAction, monoBehaviour.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
  169. }
  170. public static void BindTo<TSource, TObject>(this IUniTaskAsyncEnumerable<TSource> source, TObject bindTarget, Action<TObject, TSource> bindAction, CancellationToken cancellationToken, bool rebindOnError = true)
  171. {
  172. BindToCore(source, bindTarget, bindAction, cancellationToken, rebindOnError).Forget();
  173. }
  174. static async UniTaskVoid BindToCore<TSource, TObject>(IUniTaskAsyncEnumerable<TSource> source, TObject bindTarget, Action<TObject, TSource> bindAction, CancellationToken cancellationToken, bool rebindOnError)
  175. {
  176. var repeat = false;
  177. BIND_AGAIN:
  178. var e = source.GetAsyncEnumerator(cancellationToken);
  179. try
  180. {
  181. while (true)
  182. {
  183. bool moveNext;
  184. try
  185. {
  186. moveNext = await e.MoveNextAsync();
  187. repeat = false;
  188. }
  189. catch (Exception ex)
  190. {
  191. if (ex is OperationCanceledException) return;
  192. if (rebindOnError && !repeat)
  193. {
  194. repeat = true;
  195. goto BIND_AGAIN;
  196. }
  197. else
  198. {
  199. throw;
  200. }
  201. }
  202. if (!moveNext) return;
  203. bindAction(bindTarget, e.Current);
  204. }
  205. }
  206. finally
  207. {
  208. if (e != null)
  209. {
  210. await e.DisposeAsync();
  211. }
  212. }
  213. }
  214. }
  215. }