UniTask/Assets/UniRx.Async/UnityAsyncExtensions.uGUI.cs

401 lines
14 KiB
C#
Raw Normal View History

2020-05-04 00:59:22 +08:00
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
2019-05-19 23:14:47 +08:00
using System;
using System.Threading;
using UniRx.Async.Internal;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace UniRx.Async
{
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 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 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 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 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 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 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 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> OnValueChanged(this Dropdown dropdown)
{
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
}
public static UniTask<int> OnValueChanged(this Dropdown dropdown, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, cancellationToken, true).OnInvokeAsync();
}
}
public interface IAsyncClickEventHandler : IDisposable
{
UniTask OnClickAsync();
}
public interface IAsyncValueChangedEventHandler<T> : IDisposable
{
UniTask<T> OnValueChangedAsync();
}
public interface IAsyncEndEditEventHandler<T> : IDisposable
{
UniTask<T> OnEndEditAsync();
}
2020-05-04 00:59:22 +08:00
public class AsyncUnityEventHandler : IUniTaskSource, IDisposable, IAsyncClickEventHandler
2019-05-19 23:14:47 +08:00
{
static Action<object> cancellationCallback = CancellationCallback;
readonly UnityAction action;
readonly UnityEvent unityEvent;
2020-05-04 00:59:22 +08:00
CancellationToken cancellationToken;
2019-05-19 23:14:47 +08:00
CancellationTokenRegistration registration;
bool isDisposed;
bool callOnce;
2020-05-04 00:59:22 +08:00
UniTaskCompletionSourceCore<AsyncUnit> core;
2019-05-19 23:14:47 +08:00
public AsyncUnityEventHandler(UnityEvent unityEvent, CancellationToken cancellationToken, bool callOnce)
{
if (cancellationToken.IsCancellationRequested)
{
isDisposed = true;
return;
}
2020-05-04 00:59:22 +08:00
this.action = Invoke;
2019-05-19 23:14:47 +08:00
this.unityEvent = unityEvent;
2020-05-04 00:59:22 +08:00
this.cancellationToken = cancellationToken;
this.callOnce = callOnce;
unityEvent.AddListener(action);
2019-05-19 23:14:47 +08:00
if (cancellationToken.CanBeCanceled)
{
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
}
TaskTracker.TrackActiveTask(this, 3);
2019-05-19 23:14:47 +08:00
}
public UniTask OnInvokeAsync()
{
2020-05-04 00:59:22 +08:00
core.Reset();
return new UniTask(this, core.Version);
2019-05-19 23:14:47 +08:00
}
void Invoke()
{
2020-05-04 00:59:22 +08:00
core.TrySetResult(AsyncUnit.Default);
2019-05-19 23:14:47 +08:00
}
static void CancellationCallback(object state)
{
var self = (AsyncUnityEventHandler)state;
self.Dispose();
2020-05-04 00:59:22 +08:00
self.core.TrySetCanceled(self.cancellationToken);
2019-05-19 23:14:47 +08:00
}
public void Dispose()
{
if (!isDisposed)
{
isDisposed = true;
TaskTracker.RemoveTracking(this);
2019-05-19 23:14:47 +08:00
registration.Dispose();
if (unityEvent != null)
{
unityEvent.RemoveListener(action);
}
}
}
2020-05-04 00:59:22 +08:00
UniTask IAsyncClickEventHandler.OnClickAsync()
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
return OnInvokeAsync();
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
void IUniTaskSource.GetResult(short token)
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
try
{
core.GetResult(token);
}
finally
{
if (callOnce)
{
Dispose();
}
}
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
UniTaskStatus IUniTaskSource.GetStatus(short token)
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
return core.GetStatus(token);
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
UniTaskStatus IUniTaskSource.UnsafeGetStatus()
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
return core.UnsafeGetStatus();
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
core.OnCompleted(continuation, state, token);
2019-05-19 23:14:47 +08:00
}
}
2020-05-04 00:59:22 +08:00
public class AsyncUnityEventHandler<T> : IUniTaskSource<T>, IDisposable, IAsyncValueChangedEventHandler<T>, IAsyncEndEditEventHandler<T>
2019-05-19 23:14:47 +08:00
{
static Action<object> cancellationCallback = CancellationCallback;
readonly UnityAction<T> action;
readonly UnityEvent<T> unityEvent;
2020-05-04 00:59:22 +08:00
CancellationToken cancellationToken;
2019-05-19 23:14:47 +08:00
CancellationTokenRegistration registration;
bool isDisposed;
bool callOnce;
2020-05-04 00:59:22 +08:00
UniTaskCompletionSourceCore<T> core;
2019-05-19 23:14:47 +08:00
public AsyncUnityEventHandler(UnityEvent<T> unityEvent, CancellationToken cancellationToken, bool callOnce)
{
if (cancellationToken.IsCancellationRequested)
{
isDisposed = true;
return;
}
2020-05-04 00:59:22 +08:00
this.action = Invoke;
2019-05-19 23:14:47 +08:00
this.unityEvent = unityEvent;
2020-05-04 00:59:22 +08:00
this.cancellationToken = cancellationToken;
this.callOnce = callOnce;
unityEvent.AddListener(action);
2019-05-19 23:14:47 +08:00
if (cancellationToken.CanBeCanceled)
{
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
}
TaskTracker.TrackActiveTask(this, 3);
2019-05-19 23:14:47 +08:00
}
public UniTask<T> OnInvokeAsync()
{
2020-05-04 00:59:22 +08:00
core.Reset();
return new UniTask<T>(this, core.Version);
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
void Invoke(T result)
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
core.TrySetResult(result);
2019-05-19 23:14:47 +08:00
}
static void CancellationCallback(object state)
{
var self = (AsyncUnityEventHandler<T>)state;
self.Dispose();
2020-05-04 00:59:22 +08:00
self.core.TrySetCanceled(self.cancellationToken);
2019-05-19 23:14:47 +08:00
}
public void Dispose()
{
if (!isDisposed)
{
isDisposed = true;
TaskTracker.RemoveTracking(this);
2019-05-19 23:14:47 +08:00
registration.Dispose();
if (unityEvent != null)
{
unityEvent.RemoveListener(action);
}
}
}
2020-05-04 00:59:22 +08:00
UniTask<T> IAsyncValueChangedEventHandler<T>.OnValueChangedAsync()
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
return OnInvokeAsync();
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
UniTask<T> IAsyncEndEditEventHandler<T>.OnEndEditAsync()
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
return OnInvokeAsync();
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
T IUniTaskSource<T>.GetResult(short token)
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
try
{
return core.GetResult(token);
}
finally
{
if (callOnce)
{
Dispose();
}
}
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
void IUniTaskSource.GetResult(short token)
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
((IUniTaskSource<T>)this).GetResult(token);
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
UniTaskStatus IUniTaskSource.GetStatus(short token)
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
return core.GetStatus(token);
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
UniTaskStatus IUniTaskSource.UnsafeGetStatus()
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
return core.UnsafeGetStatus();
2019-05-19 23:14:47 +08:00
}
2020-05-04 00:59:22 +08:00
void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
2019-05-19 23:14:47 +08:00
{
2020-05-04 00:59:22 +08:00
core.OnCompleted(continuation, state, token);
2019-05-19 23:14:47 +08:00
}
}
2020-05-04 00:59:22 +08:00
}