From 3b593f349cc626f3f043a520f3dac4cb09338eb1 Mon Sep 17 00:00:00 2001 From: neuecc Date: Thu, 21 May 2020 02:26:09 +0900 Subject: [PATCH] Add UnityEvent and InputField AsyncEventHandler and extensions --- .../Runtime/UnityAsyncExtensions.uGUI.cs | 48 +++++- src/UniTask/Assets/Scenes/SandboxMain.cs | 140 +++++++++++++++--- 2 files changed, 167 insertions(+), 21 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs index c107e9f2..1ab4f1c5 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs @@ -1,9 +1,8 @@ #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +using Cysharp.Threading.Tasks.Linq; using System; using System.Threading; -using Cysharp.Threading.Tasks.Internal; -using Cysharp.Threading.Tasks.Linq; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; @@ -27,6 +26,21 @@ public static IUniTaskAsyncEnumerable OnInvokeAsAsyncEnumerable(this return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken); } + 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 OnInvokeAsAsyncEnumerable(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken); + } + public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button) { return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false); @@ -207,6 +221,36 @@ public static IUniTaskAsyncEnumerable OnEndEditAsAsyncEnumerable(this In return new UnityEventHandlerAsyncEnumerable(inputField.onEndEdit, cancellationToken); } + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, cancellationToken, false); + } + + public static UniTask OnValueChangedAsync(this InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnValueChangedAsync(this InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onValueChanged, cancellationToken); + } + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Dropdown dropdown) { return new AsyncUnityEventHandler(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false); diff --git a/src/UniTask/Assets/Scenes/SandboxMain.cs b/src/UniTask/Assets/Scenes/SandboxMain.cs index bbf75bb3..825bc0df 100644 --- a/src/UniTask/Assets/Scenes/SandboxMain.cs +++ b/src/UniTask/Assets/Scenes/SandboxMain.cs @@ -35,8 +35,33 @@ public enum MyEnum } +public class SimplePresenter +{ + // View + public UnityEngine.UI.InputField Input; + + + // Presenter + public SimplePresenter() + { + //Input.OnValueChangedAsAsyncEnumerable() + // .Queue() + // .SelectAwait(async x => + // { + // await UniTask.Delay(TimeSpan.FromSeconds(1)); + // return x; + // }) + // .Select(x=> x.ToUpper()) + // .BindTo( + + + + } + +} + @@ -87,7 +112,7 @@ public class SandboxMain : MonoBehaviour { public Button okButton; public Button cancelButton; - public Text text; + CancellationTokenSource cts; @@ -110,6 +135,63 @@ async UniTask FooAsync() } + + + + public class Model + { + // State Hp { get; } + + AsyncReactiveProperty hp; + IReadOnlyAsyncReactiveProperty Hp => hp; + + + + public Model() + { + // hp = new AsyncReactiveProperty(); + + + + + + + //setHp = Hp.GetSetter(); + } + + void Increment(int value) + { + + + // setHp(Hp.Value += value); + } + } + + + + public Text text; + public Button button; + + [SerializeField] + State count; + + void Start2() + { + count = 10; + + var countS = count.GetSetter(); + + count.BindTo(text); + button.OnClickAsAsyncEnumerable().ForEachAsync(_ => + { + // int foo = countS; + //countS.Set(countS += 10); + + // setter.SetValue(count.Value + 10); + }); + } + + async UniTask RunStandardDelayAsync() { UnityEngine.Debug.Log("DEB"); @@ -126,10 +208,6 @@ async UniTask RunJobAsync() var scheduled = job.Schedule(); - - - - UnityEngine.Debug.Log("OK"); await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update); UnityEngine.Debug.Log("OK2"); @@ -170,40 +248,64 @@ await UniTaskAsyncEnumerable Debug.Log("Done"); } - void Start() + public int MyProperty { get; set; } + + public class MyClass { - //var rp = new AsyncReactiveProperty(10); + public int MyProperty { get; set; } + } - //Running(rp).Forget(); + MyClass mcc; - //await UniTaskAsyncEnumerable.EveryUpdate().Take(10).ForEachAsync((x, i) => rp.Value = i); + void Start() + { + this.mcc = new MyClass(); + this.MyProperty = 999; - //rp.Dispose(); + CheckDest().Forget(); - //var channel = Channel.CreateSingleConsumerUnbounded(); - //Debug.Log("wait channel"); - //await channel.Reader.ReadAllAsync(this.GetCancellationTokenOnDestroy()).ForEachAsync(_ => { }); + //UniTaskAsyncEnumerable.EveryValueChanged(mcc, x => x.MyProperty) + // .Do(_ => { }, () => Debug.Log("COMPLETED")) + // .ForEachAsync(x => + // { + // Debug.Log("VALUE_CHANGED:" + x); + // }) + // .Forget(); - var pubsub = new AsyncMessageBroker(); - pubsub.Subscribe().ForEachAsync(x => Debug.Log("A:" + x)).Forget(); - pubsub.Subscribe().ForEachAsync(x => Debug.Log("B:" + x)).Forget(); - int i = 0; okButton.OnClickAsAsyncEnumerable().ForEachAsync(_ => { - Debug.Log("foo"); - pubsub.Publish(i++); + mcc.MyProperty += 10; + }).Forget(); + cancelButton.OnClickAsAsyncEnumerable().ForEachAsync(_ => + { + this.mcc = null; + }); } + async UniTaskVoid CheckDest() + { + try + { + Debug.Log("WAIT"); + await UniTask.WaitUntilValueChanged(mcc, x => x.MyProperty); + Debug.Log("CHANGED?"); + } + finally + { + Debug.Log("END"); + } + } + async UniTaskVoid Running(CancellationToken ct) { Debug.Log("BEGIN");