123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858 |
- #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
- #if !UNITY_2019_1_OR_NEWER || UNITASK_UGUI_SUPPORT
- using System;
- using System.Threading;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.UI;
- namespace Cysharp.Threading.Tasks
- {
- public static partial class UnityAsyncExtensions
- {
- public static AsyncUnityEventHandler GetAsyncEventHandler(this UnityEvent unityEvent, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler(unityEvent, cancellationToken, false);
- }
- public static UniTask OnInvokeAsync(this UnityEvent unityEvent, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler(unityEvent, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<AsyncUnit> OnInvokeAsAsyncEnumerable(this UnityEvent unityEvent, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken);
- }
- public static AsyncUnityEventHandler<T> GetAsyncEventHandler<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<T>(unityEvent, cancellationToken, false);
- }
- public static UniTask<T> OnInvokeAsync<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<T>(unityEvent, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<T> OnInvokeAsAsyncEnumerable<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable<T>(unityEvent, cancellationToken);
- }
- public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button)
- {
- return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false);
- }
- public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler(button.onClick, cancellationToken, false);
- }
- public static UniTask OnClickAsync(this Button button)
- {
- return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
- }
- public static UniTask OnClickAsync(this Button button, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler(button.onClick, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<AsyncUnit> OnClickAsAsyncEnumerable(this Button button)
- {
- return new UnityEventHandlerAsyncEnumerable(button.onClick, button.GetCancellationTokenOnDestroy());
- }
- public static IUniTaskAsyncEnumerable<AsyncUnit> OnClickAsAsyncEnumerable(this Button button, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable(button.onClick, cancellationToken);
- }
- public static IAsyncValueChangedEventHandler<bool> GetAsyncValueChangedEventHandler(this Toggle toggle)
- {
- return new AsyncUnityEventHandler<bool>(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy(), false);
- }
- public static IAsyncValueChangedEventHandler<bool> GetAsyncValueChangedEventHandler(this Toggle toggle, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<bool>(toggle.onValueChanged, cancellationToken, false);
- }
- public static UniTask<bool> OnValueChangedAsync(this Toggle toggle)
- {
- return new AsyncUnityEventHandler<bool>(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
- }
- public static UniTask<bool> OnValueChangedAsync(this Toggle toggle, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<bool>(toggle.onValueChanged, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<bool> OnValueChangedAsAsyncEnumerable(this Toggle toggle)
- {
- return new UnityEventHandlerAsyncEnumerable<bool>(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy());
- }
- public static IUniTaskAsyncEnumerable<bool> OnValueChangedAsAsyncEnumerable(this Toggle toggle, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable<bool>(toggle.onValueChanged, cancellationToken);
- }
- public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHandler(this Scrollbar scrollbar)
- {
- return new AsyncUnityEventHandler<float>(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy(), false);
- }
- public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHandler(this Scrollbar scrollbar, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<float>(scrollbar.onValueChanged, cancellationToken, false);
- }
- public static UniTask<float> OnValueChangedAsync(this Scrollbar scrollbar)
- {
- return new AsyncUnityEventHandler<float>(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
- }
- public static UniTask<float> OnValueChangedAsync(this Scrollbar scrollbar, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<float>(scrollbar.onValueChanged, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<float> OnValueChangedAsAsyncEnumerable(this Scrollbar scrollbar)
- {
- return new UnityEventHandlerAsyncEnumerable<float>(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy());
- }
- public static IUniTaskAsyncEnumerable<float> OnValueChangedAsAsyncEnumerable(this Scrollbar scrollbar, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable<float>(scrollbar.onValueChanged, cancellationToken);
- }
- public static IAsyncValueChangedEventHandler<Vector2> GetAsyncValueChangedEventHandler(this ScrollRect scrollRect)
- {
- return new AsyncUnityEventHandler<Vector2>(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy(), false);
- }
- public static IAsyncValueChangedEventHandler<Vector2> GetAsyncValueChangedEventHandler(this ScrollRect scrollRect, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<Vector2>(scrollRect.onValueChanged, cancellationToken, false);
- }
- public static UniTask<Vector2> OnValueChangedAsync(this ScrollRect scrollRect)
- {
- return new AsyncUnityEventHandler<Vector2>(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
- }
- public static UniTask<Vector2> OnValueChangedAsync(this ScrollRect scrollRect, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<Vector2>(scrollRect.onValueChanged, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<Vector2> OnValueChangedAsAsyncEnumerable(this ScrollRect scrollRect)
- {
- return new UnityEventHandlerAsyncEnumerable<Vector2>(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy());
- }
- public static IUniTaskAsyncEnumerable<Vector2> OnValueChangedAsAsyncEnumerable(this ScrollRect scrollRect, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable<Vector2>(scrollRect.onValueChanged, cancellationToken);
- }
- public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHandler(this Slider slider)
- {
- return new AsyncUnityEventHandler<float>(slider.onValueChanged, slider.GetCancellationTokenOnDestroy(), false);
- }
- public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHandler(this Slider slider, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<float>(slider.onValueChanged, cancellationToken, false);
- }
- public static UniTask<float> OnValueChangedAsync(this Slider slider)
- {
- return new AsyncUnityEventHandler<float>(slider.onValueChanged, slider.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
- }
- public static UniTask<float> OnValueChangedAsync(this Slider slider, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<float>(slider.onValueChanged, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<float> OnValueChangedAsAsyncEnumerable(this Slider slider)
- {
- return new UnityEventHandlerAsyncEnumerable<float>(slider.onValueChanged, slider.GetCancellationTokenOnDestroy());
- }
- public static IUniTaskAsyncEnumerable<float> OnValueChangedAsAsyncEnumerable(this Slider slider, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable<float>(slider.onValueChanged, cancellationToken);
- }
- public static IAsyncEndEditEventHandler<string> GetAsyncEndEditEventHandler(this InputField inputField)
- {
- return new AsyncUnityEventHandler<string>(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy(), false);
- }
- public static IAsyncEndEditEventHandler<string> GetAsyncEndEditEventHandler(this InputField inputField, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<string>(inputField.onEndEdit, cancellationToken, false);
- }
- public static UniTask<string> OnEndEditAsync(this InputField inputField)
- {
- return new AsyncUnityEventHandler<string>(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
- }
- public static UniTask<string> OnEndEditAsync(this InputField inputField, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<string>(inputField.onEndEdit, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<string> OnEndEditAsAsyncEnumerable(this InputField inputField)
- {
- return new UnityEventHandlerAsyncEnumerable<string>(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy());
- }
- public static IUniTaskAsyncEnumerable<string> OnEndEditAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable<string>(inputField.onEndEdit, cancellationToken);
- }
- public static IAsyncValueChangedEventHandler<string> GetAsyncValueChangedEventHandler(this InputField inputField)
- {
- return new AsyncUnityEventHandler<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), false);
- }
- public static IAsyncValueChangedEventHandler<string> GetAsyncValueChangedEventHandler(this InputField inputField, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<string>(inputField.onValueChanged, cancellationToken, false);
- }
- public static UniTask<string> OnValueChangedAsync(this InputField inputField)
- {
- return new AsyncUnityEventHandler<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
- }
- public static UniTask<string> OnValueChangedAsync(this InputField inputField, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<string>(inputField.onValueChanged, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<string> OnValueChangedAsAsyncEnumerable(this InputField inputField)
- {
- return new UnityEventHandlerAsyncEnumerable<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy());
- }
- public static IUniTaskAsyncEnumerable<string> OnValueChangedAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable<string>(inputField.onValueChanged, cancellationToken);
- }
- public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown)
- {
- return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
- }
- public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, cancellationToken, false);
- }
- public static UniTask<int> OnValueChangedAsync(this Dropdown dropdown)
- {
- return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
- }
- public static UniTask<int> OnValueChangedAsync(this Dropdown dropdown, CancellationToken cancellationToken)
- {
- return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, cancellationToken, true).OnInvokeAsync();
- }
- public static IUniTaskAsyncEnumerable<int> OnValueChangedAsAsyncEnumerable(this Dropdown dropdown)
- {
- return new UnityEventHandlerAsyncEnumerable<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy());
- }
- public static IUniTaskAsyncEnumerable<int> OnValueChangedAsAsyncEnumerable(this Dropdown dropdown, CancellationToken cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerable<int>(dropdown.onValueChanged, cancellationToken);
- }
- }
- public interface IAsyncClickEventHandler : IDisposable
- {
- UniTask OnClickAsync();
- }
- public interface IAsyncValueChangedEventHandler<T> : IDisposable
- {
- UniTask<T> OnValueChangedAsync();
- }
- public interface IAsyncEndEditEventHandler<T> : IDisposable
- {
- UniTask<T> OnEndEditAsync();
- }
- // for TMP_PRO
- public interface IAsyncEndTextSelectionEventHandler<T> : IDisposable
- {
- UniTask<T> OnEndTextSelectionAsync();
- }
- public interface IAsyncTextSelectionEventHandler<T> : IDisposable
- {
- UniTask<T> OnTextSelectionAsync();
- }
- public interface IAsyncDeselectEventHandler<T> : IDisposable
- {
- UniTask<T> OnDeselectAsync();
- }
- public interface IAsyncSelectEventHandler<T> : IDisposable
- {
- UniTask<T> OnSelectAsync();
- }
- public interface IAsyncSubmitEventHandler<T> : IDisposable
- {
- UniTask<T> OnSubmitAsync();
- }
- internal class TextSelectionEventConverter : UnityEvent<(string, int, int)>, IDisposable
- {
- readonly UnityEvent<string, int, int> innerEvent;
- readonly UnityAction<string, int, int> invokeDelegate;
- public TextSelectionEventConverter(UnityEvent<string, int, int> unityEvent)
- {
- this.innerEvent = unityEvent;
- this.invokeDelegate = InvokeCore;
- innerEvent.AddListener(invokeDelegate);
- }
- void InvokeCore(string item1, int item2, int item3)
- {
- innerEvent.Invoke(item1, item2, item3);
- }
- public void Dispose()
- {
- innerEvent.RemoveListener(invokeDelegate);
- }
- }
- public class AsyncUnityEventHandler : IUniTaskSource, IDisposable, IAsyncClickEventHandler
- {
- static Action<object> cancellationCallback = CancellationCallback;
- readonly UnityAction action;
- readonly UnityEvent unityEvent;
- CancellationToken cancellationToken;
- CancellationTokenRegistration registration;
- bool isDisposed;
- bool callOnce;
- UniTaskCompletionSourceCore<AsyncUnit> core;
- public AsyncUnityEventHandler(UnityEvent unityEvent, CancellationToken cancellationToken, bool callOnce)
- {
- this.cancellationToken = cancellationToken;
- if (cancellationToken.IsCancellationRequested)
- {
- isDisposed = true;
- return;
- }
- this.action = Invoke;
- this.unityEvent = unityEvent;
- this.callOnce = callOnce;
- unityEvent.AddListener(action);
- if (cancellationToken.CanBeCanceled)
- {
- registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
- }
- TaskTracker.TrackActiveTask(this, 3);
- }
- public UniTask OnInvokeAsync()
- {
- core.Reset();
- if (isDisposed)
- {
- core.TrySetCanceled(this.cancellationToken);
- }
- return new UniTask(this, core.Version);
- }
- void Invoke()
- {
- core.TrySetResult(AsyncUnit.Default);
- }
- static void CancellationCallback(object state)
- {
- var self = (AsyncUnityEventHandler)state;
- self.Dispose();
- }
- public void Dispose()
- {
- if (!isDisposed)
- {
- isDisposed = true;
- TaskTracker.RemoveTracking(this);
- registration.Dispose();
- if (unityEvent != null)
- {
- unityEvent.RemoveListener(action);
- }
- core.TrySetCanceled(cancellationToken);
- }
- }
- UniTask IAsyncClickEventHandler.OnClickAsync()
- {
- return OnInvokeAsync();
- }
- void IUniTaskSource.GetResult(short token)
- {
- try
- {
- core.GetResult(token);
- }
- finally
- {
- if (callOnce)
- {
- Dispose();
- }
- }
- }
- UniTaskStatus IUniTaskSource.GetStatus(short token)
- {
- return core.GetStatus(token);
- }
- UniTaskStatus IUniTaskSource.UnsafeGetStatus()
- {
- return core.UnsafeGetStatus();
- }
- void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
- {
- core.OnCompleted(continuation, state, token);
- }
- }
- public class AsyncUnityEventHandler<T> : IUniTaskSource<T>, IDisposable, IAsyncValueChangedEventHandler<T>, IAsyncEndEditEventHandler<T>
- , IAsyncEndTextSelectionEventHandler<T>, IAsyncTextSelectionEventHandler<T>, IAsyncDeselectEventHandler<T>, IAsyncSelectEventHandler<T>, IAsyncSubmitEventHandler<T>
- {
- static Action<object> cancellationCallback = CancellationCallback;
- readonly UnityAction<T> action;
- readonly UnityEvent<T> unityEvent;
- CancellationToken cancellationToken;
- CancellationTokenRegistration registration;
- bool isDisposed;
- bool callOnce;
- UniTaskCompletionSourceCore<T> core;
- public AsyncUnityEventHandler(UnityEvent<T> unityEvent, CancellationToken cancellationToken, bool callOnce)
- {
- this.cancellationToken = cancellationToken;
- if (cancellationToken.IsCancellationRequested)
- {
- isDisposed = true;
- return;
- }
- this.action = Invoke;
- this.unityEvent = unityEvent;
- this.callOnce = callOnce;
- unityEvent.AddListener(action);
- if (cancellationToken.CanBeCanceled)
- {
- registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
- }
- TaskTracker.TrackActiveTask(this, 3);
- }
- public UniTask<T> OnInvokeAsync()
- {
- core.Reset();
- if (isDisposed)
- {
- core.TrySetCanceled(this.cancellationToken);
- }
- return new UniTask<T>(this, core.Version);
- }
- void Invoke(T result)
- {
- core.TrySetResult(result);
- }
- static void CancellationCallback(object state)
- {
- var self = (AsyncUnityEventHandler<T>)state;
- self.Dispose();
- }
- public void Dispose()
- {
- if (!isDisposed)
- {
- isDisposed = true;
- TaskTracker.RemoveTracking(this);
- registration.Dispose();
- if (unityEvent != null)
- {
- // Dispose inner delegate for TextSelectionEventConverter
- if (unityEvent is IDisposable disp)
- {
- disp.Dispose();
- }
- unityEvent.RemoveListener(action);
- }
- core.TrySetCanceled();
- }
- }
- UniTask<T> IAsyncValueChangedEventHandler<T>.OnValueChangedAsync()
- {
- return OnInvokeAsync();
- }
- UniTask<T> IAsyncEndEditEventHandler<T>.OnEndEditAsync()
- {
- return OnInvokeAsync();
- }
- UniTask<T> IAsyncEndTextSelectionEventHandler<T>.OnEndTextSelectionAsync()
- {
- return OnInvokeAsync();
- }
- UniTask<T> IAsyncTextSelectionEventHandler<T>.OnTextSelectionAsync()
- {
- return OnInvokeAsync();
- }
- UniTask<T> IAsyncDeselectEventHandler<T>.OnDeselectAsync()
- {
- return OnInvokeAsync();
- }
- UniTask<T> IAsyncSelectEventHandler<T>.OnSelectAsync()
- {
- return OnInvokeAsync();
- }
- UniTask<T> IAsyncSubmitEventHandler<T>.OnSubmitAsync()
- {
- return OnInvokeAsync();
- }
- T IUniTaskSource<T>.GetResult(short token)
- {
- try
- {
- return core.GetResult(token);
- }
- finally
- {
- if (callOnce)
- {
- Dispose();
- }
- }
- }
- void IUniTaskSource.GetResult(short token)
- {
- ((IUniTaskSource<T>)this).GetResult(token);
- }
- UniTaskStatus IUniTaskSource.GetStatus(short token)
- {
- return core.GetStatus(token);
- }
- UniTaskStatus IUniTaskSource.UnsafeGetStatus()
- {
- return core.UnsafeGetStatus();
- }
- void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
- {
- core.OnCompleted(continuation, state, token);
- }
- }
- public class UnityEventHandlerAsyncEnumerable : IUniTaskAsyncEnumerable<AsyncUnit>
- {
- readonly UnityEvent unityEvent;
- readonly CancellationToken cancellationToken1;
- public UnityEventHandlerAsyncEnumerable(UnityEvent unityEvent, CancellationToken cancellationToken)
- {
- this.unityEvent = unityEvent;
- this.cancellationToken1 = cancellationToken;
- }
- public IUniTaskAsyncEnumerator<AsyncUnit> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- if (this.cancellationToken1 == cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerator(unityEvent, this.cancellationToken1, CancellationToken.None);
- }
- else
- {
- return new UnityEventHandlerAsyncEnumerator(unityEvent, this.cancellationToken1, cancellationToken);
- }
- }
- class UnityEventHandlerAsyncEnumerator : MoveNextSource, IUniTaskAsyncEnumerator<AsyncUnit>
- {
- static readonly Action<object> cancel1 = OnCanceled1;
- static readonly Action<object> cancel2 = OnCanceled2;
- readonly UnityEvent unityEvent;
- CancellationToken cancellationToken1;
- CancellationToken cancellationToken2;
- UnityAction unityAction;
- CancellationTokenRegistration registration1;
- CancellationTokenRegistration registration2;
- bool isDisposed;
- public UnityEventHandlerAsyncEnumerator(UnityEvent unityEvent, CancellationToken cancellationToken1, CancellationToken cancellationToken2)
- {
- this.unityEvent = unityEvent;
- this.cancellationToken1 = cancellationToken1;
- this.cancellationToken2 = cancellationToken2;
- }
- public AsyncUnit Current => default;
- public UniTask<bool> MoveNextAsync()
- {
- cancellationToken1.ThrowIfCancellationRequested();
- cancellationToken2.ThrowIfCancellationRequested();
- completionSource.Reset();
- if (unityAction == null)
- {
- unityAction = Invoke;
- TaskTracker.TrackActiveTask(this, 3);
- unityEvent.AddListener(unityAction);
- if (cancellationToken1.CanBeCanceled)
- {
- registration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel1, this);
- }
- if (cancellationToken2.CanBeCanceled)
- {
- registration2 = cancellationToken2.RegisterWithoutCaptureExecutionContext(cancel2, this);
- }
- }
- return new UniTask<bool>(this, completionSource.Version);
- }
- void Invoke()
- {
- completionSource.TrySetResult(true);
- }
- static void OnCanceled1(object state)
- {
- var self = (UnityEventHandlerAsyncEnumerator)state;
- try
- {
- self.completionSource.TrySetCanceled(self.cancellationToken1);
- }
- finally
- {
- self.DisposeAsync().Forget();
- }
- }
- static void OnCanceled2(object state)
- {
- var self = (UnityEventHandlerAsyncEnumerator)state;
- try
- {
- self.completionSource.TrySetCanceled(self.cancellationToken2);
- }
- finally
- {
- self.DisposeAsync().Forget();
- }
- }
- public UniTask DisposeAsync()
- {
- if (!isDisposed)
- {
- isDisposed = true;
- TaskTracker.RemoveTracking(this);
- registration1.Dispose();
- registration2.Dispose();
- unityEvent.RemoveListener(unityAction);
- completionSource.TrySetCanceled();
- }
- return default;
- }
- }
- }
- public class UnityEventHandlerAsyncEnumerable<T> : IUniTaskAsyncEnumerable<T>
- {
- readonly UnityEvent<T> unityEvent;
- readonly CancellationToken cancellationToken1;
- public UnityEventHandlerAsyncEnumerable(UnityEvent<T> unityEvent, CancellationToken cancellationToken)
- {
- this.unityEvent = unityEvent;
- this.cancellationToken1 = cancellationToken;
- }
- public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- if (this.cancellationToken1 == cancellationToken)
- {
- return new UnityEventHandlerAsyncEnumerator(unityEvent, this.cancellationToken1, CancellationToken.None);
- }
- else
- {
- return new UnityEventHandlerAsyncEnumerator(unityEvent, this.cancellationToken1, cancellationToken);
- }
- }
- class UnityEventHandlerAsyncEnumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>
- {
- static readonly Action<object> cancel1 = OnCanceled1;
- static readonly Action<object> cancel2 = OnCanceled2;
- readonly UnityEvent<T> unityEvent;
- CancellationToken cancellationToken1;
- CancellationToken cancellationToken2;
- UnityAction<T> unityAction;
- CancellationTokenRegistration registration1;
- CancellationTokenRegistration registration2;
- bool isDisposed;
- public UnityEventHandlerAsyncEnumerator(UnityEvent<T> unityEvent, CancellationToken cancellationToken1, CancellationToken cancellationToken2)
- {
- this.unityEvent = unityEvent;
- this.cancellationToken1 = cancellationToken1;
- this.cancellationToken2 = cancellationToken2;
- }
- public T Current { get; private set; }
- public UniTask<bool> MoveNextAsync()
- {
- cancellationToken1.ThrowIfCancellationRequested();
- cancellationToken2.ThrowIfCancellationRequested();
- completionSource.Reset();
- if (unityAction == null)
- {
- unityAction = Invoke;
- TaskTracker.TrackActiveTask(this, 3);
- unityEvent.AddListener(unityAction);
- if (cancellationToken1.CanBeCanceled)
- {
- registration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel1, this);
- }
- if (cancellationToken2.CanBeCanceled)
- {
- registration2 = cancellationToken2.RegisterWithoutCaptureExecutionContext(cancel2, this);
- }
- }
- return new UniTask<bool>(this, completionSource.Version);
- }
- void Invoke(T value)
- {
- Current = value;
- completionSource.TrySetResult(true);
- }
- static void OnCanceled1(object state)
- {
- var self = (UnityEventHandlerAsyncEnumerator)state;
- try
- {
- self.completionSource.TrySetCanceled(self.cancellationToken1);
- }
- finally
- {
- self.DisposeAsync().Forget();
- }
- }
- static void OnCanceled2(object state)
- {
- var self = (UnityEventHandlerAsyncEnumerator)state;
- try
- {
- self.completionSource.TrySetCanceled(self.cancellationToken2);
- }
- finally
- {
- self.DisposeAsync().Forget();
- }
- }
- public UniTask DisposeAsync()
- {
- if (!isDisposed)
- {
- isDisposed = true;
- TaskTracker.RemoveTracking(this);
- registration1.Dispose();
- registration2.Dispose();
- if (unityEvent is IDisposable disp)
- {
- disp.Dispose();
- }
- unityEvent.RemoveListener(unityAction);
- completionSource.TrySetCanceled();
- }
- return default;
- }
- }
- }
- }
- #endif
|