123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- using System;
- using System.Threading;
- using UnityEngine;
- #if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT
- using UnityEngine.UI;
- #endif
- namespace Cysharp.Threading.Tasks
- {
- public static class UnityBindingExtensions
- {
- #if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT
- // <string> -> Text
- public static void BindTo(this IUniTaskAsyncEnumerable<string> source, UnityEngine.UI.Text text, bool rebindOnError = true)
- {
- BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
- }
- public static void BindTo(this IUniTaskAsyncEnumerable<string> source, UnityEngine.UI.Text text, CancellationToken cancellationToken, bool rebindOnError = true)
- {
- BindToCore(source, text, cancellationToken, rebindOnError).Forget();
- }
- static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable<string> source, UnityEngine.UI.Text text, CancellationToken cancellationToken, bool rebindOnError)
- {
- var repeat = false;
- BIND_AGAIN:
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (true)
- {
- bool moveNext;
- try
- {
- moveNext = await e.MoveNextAsync();
- repeat = false;
- }
- catch (Exception ex)
- {
- if (ex is OperationCanceledException) return;
- if (rebindOnError && !repeat)
- {
- repeat = true;
- goto BIND_AGAIN;
- }
- else
- {
- throw;
- }
- }
- if (!moveNext) return;
- text.text = e.Current;
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- // <T> -> Text
- public static void BindTo<T>(this IUniTaskAsyncEnumerable<T> source, UnityEngine.UI.Text text, bool rebindOnError = true)
- {
- BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
- }
- public static void BindTo<T>(this IUniTaskAsyncEnumerable<T> source, UnityEngine.UI.Text text, CancellationToken cancellationToken, bool rebindOnError = true)
- {
- BindToCore(source, text, cancellationToken, rebindOnError).Forget();
- }
- public static void BindTo<T>(this AsyncReactiveProperty<T> source, UnityEngine.UI.Text text, bool rebindOnError = true)
- {
- BindToCore(source, text, text.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
- }
- static async UniTaskVoid BindToCore<T>(IUniTaskAsyncEnumerable<T> source, UnityEngine.UI.Text text, CancellationToken cancellationToken, bool rebindOnError)
- {
- var repeat = false;
- BIND_AGAIN:
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (true)
- {
- bool moveNext;
- try
- {
- moveNext = await e.MoveNextAsync();
- repeat = false;
- }
- catch (Exception ex)
- {
- if (ex is OperationCanceledException) return;
- if (rebindOnError && !repeat)
- {
- repeat = true;
- goto BIND_AGAIN;
- }
- else
- {
- throw;
- }
- }
- if (!moveNext) return;
- text.text = e.Current.ToString();
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- // <bool> -> Selectable
- public static void BindTo(this IUniTaskAsyncEnumerable<bool> source, Selectable selectable, bool rebindOnError = true)
- {
- BindToCore(source, selectable, selectable.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
- }
- public static void BindTo(this IUniTaskAsyncEnumerable<bool> source, Selectable selectable, CancellationToken cancellationToken, bool rebindOnError = true)
- {
- BindToCore(source, selectable, cancellationToken, rebindOnError).Forget();
- }
- static async UniTaskVoid BindToCore(IUniTaskAsyncEnumerable<bool> source, Selectable selectable, CancellationToken cancellationToken, bool rebindOnError)
- {
- var repeat = false;
- BIND_AGAIN:
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (true)
- {
- bool moveNext;
- try
- {
- moveNext = await e.MoveNextAsync();
- repeat = false;
- }
- catch (Exception ex)
- {
- if (ex is OperationCanceledException) return;
- if (rebindOnError && !repeat)
- {
- repeat = true;
- goto BIND_AGAIN;
- }
- else
- {
- throw;
- }
- }
- if (!moveNext) return;
- selectable.interactable = e.Current;
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- #endif
- // <T> -> Action
- public static void BindTo<TSource, TObject>(this IUniTaskAsyncEnumerable<TSource> source, TObject monoBehaviour, Action<TObject, TSource> bindAction, bool rebindOnError = true)
- where TObject : MonoBehaviour
- {
- BindToCore(source, monoBehaviour, bindAction, monoBehaviour.GetCancellationTokenOnDestroy(), rebindOnError).Forget();
- }
- public static void BindTo<TSource, TObject>(this IUniTaskAsyncEnumerable<TSource> source, TObject bindTarget, Action<TObject, TSource> bindAction, CancellationToken cancellationToken, bool rebindOnError = true)
- {
- BindToCore(source, bindTarget, bindAction, cancellationToken, rebindOnError).Forget();
- }
- static async UniTaskVoid BindToCore<TSource, TObject>(IUniTaskAsyncEnumerable<TSource> source, TObject bindTarget, Action<TObject, TSource> bindAction, CancellationToken cancellationToken, bool rebindOnError)
- {
- var repeat = false;
- BIND_AGAIN:
- var e = source.GetAsyncEnumerator(cancellationToken);
- try
- {
- while (true)
- {
- bool moveNext;
- try
- {
- moveNext = await e.MoveNextAsync();
- repeat = false;
- }
- catch (Exception ex)
- {
- if (ex is OperationCanceledException) return;
- if (rebindOnError && !repeat)
- {
- repeat = true;
- goto BIND_AGAIN;
- }
- else
- {
- throw;
- }
- }
- if (!moveNext) return;
- bindAction(bindTarget, e.Current);
- }
- }
- finally
- {
- if (e != null)
- {
- await e.DisposeAsync();
- }
- }
- }
- }
- }
|