You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

130 lines
4.1 KiB

  1. #if UNITASK_TEXTMESHPRO_SUPPORT
  2. using System;
  3. using System.Threading;
  4. using TMPro;
  5. using UnityEngine.Events;
  6. namespace Cysharp.Threading.Tasks
  7. {
  8. public static partial class TextMeshProAsyncExtensions
  9. {
  10. // <string> -> Text
  11. public static void BindTo(this IUniTaskAsyncEnumerable<string> source, TMP_Text text, bool rebindOnError = true)
  12. {
  13. BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
  14. }
  15. public static void BindTo(this IUniTaskAsyncEnumerable<string> source, TMP_Text text, CancellationToken cancellationToken, bool rebindOnError = true)
  16. {
  17. BindToCore(source, text, cancellationToken, rebindOnError).Forget();
  18. }
  19. static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable<string> source, TMP_Text text, CancellationToken cancellationToken, bool rebindOnError)
  20. {
  21. var repeat = false;
  22. BIND_AGAIN:
  23. var e = source.GetAsyncEnumerator(cancellationToken);
  24. try
  25. {
  26. while (true)
  27. {
  28. bool moveNext;
  29. try
  30. {
  31. moveNext = await e.MoveNextAsync();
  32. repeat = false;
  33. }
  34. catch (Exception ex)
  35. {
  36. if (ex is OperationCanceledException) return;
  37. if (rebindOnError && !repeat)
  38. {
  39. repeat = true;
  40. goto BIND_AGAIN;
  41. }
  42. else
  43. {
  44. throw;
  45. }
  46. }
  47. if (!moveNext) return;
  48. text.text = e.Current;
  49. }
  50. }
  51. finally
  52. {
  53. if (e != null)
  54. {
  55. await e.DisposeAsync();
  56. }
  57. }
  58. }
  59. // <T> -> Text
  60. public static void BindTo<T>(this IUniTaskAsyncEnumerable<T> source, TMP_Text text, bool rebindOnError = true)
  61. {
  62. BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
  63. }
  64. public static void BindTo<T>(this IUniTaskAsyncEnumerable<T> source, TMP_Text text, CancellationToken cancellationToken, bool rebindOnError = true)
  65. {
  66. BindToCore(source, text, cancellationToken, rebindOnError).Forget();
  67. }
  68. public static void BindTo<T>(this AsyncReactiveProperty<T> source, TMP_Text text, bool rebindOnError = true)
  69. {
  70. BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
  71. }
  72. static async UniTaskVoid BindToCore<T>(IUniTaskAsyncEnumerable<T> source, TMP_Text text, CancellationToken cancellationToken, bool rebindOnError)
  73. {
  74. var repeat = false;
  75. BIND_AGAIN:
  76. var e = source.GetAsyncEnumerator(cancellationToken);
  77. try
  78. {
  79. while (true)
  80. {
  81. bool moveNext;
  82. try
  83. {
  84. moveNext = await e.MoveNextAsync();
  85. repeat = false;
  86. }
  87. catch (Exception ex)
  88. {
  89. if (ex is OperationCanceledException) return;
  90. if (rebindOnError && !repeat)
  91. {
  92. repeat = true;
  93. goto BIND_AGAIN;
  94. }
  95. else
  96. {
  97. throw;
  98. }
  99. }
  100. if (!moveNext) return;
  101. text.text = e.Current.ToString();
  102. }
  103. }
  104. finally
  105. {
  106. if (e != null)
  107. {
  108. await e.DisposeAsync();
  109. }
  110. }
  111. }
  112. }
  113. }
  114. #endif