From 37e8b4500e669a014a113030551a7e5e16ca70d2 Mon Sep 17 00:00:00 2001 From: neuecc Date: Tue, 22 Sep 2020 10:08:03 +0900 Subject: [PATCH] Add enumerator.ToUniTask(MonoBehaviour coroutineRunner), log WARN on await enumerator when yield not supported types(Coroutine, WaitForEndOfFrame, WaitForFixedUpdate). --- .../Runtime/EnumeratorAsyncExtensions.cs | 28 +++++++++++++++---- src/UniTask/Assets/Scenes/SandboxMain.cs | 11 ++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/EnumeratorAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/EnumeratorAsyncExtensions.cs index 7e3ebe7..831a3fc 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/EnumeratorAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/EnumeratorAsyncExtensions.cs @@ -32,6 +32,19 @@ namespace Cysharp.Threading.Tasks return new UniTask(EnumeratorPromise.Create(enumerator, timing, cancellationToken, out var token), token); } + public static UniTask ToUniTask(this IEnumerator enumerator, MonoBehaviour coroutineRunner) + { + var source = AutoResetUniTaskCompletionSource.Create(); + coroutineRunner.StartCoroutine(Core(enumerator, coroutineRunner, source)); + return source.Task; + } + + static IEnumerator Core(IEnumerator inner, MonoBehaviour coroutineRunner, AutoResetUniTaskCompletionSource source) + { + yield return coroutineRunner.StartCoroutine(inner); + source.TrySetResult(); + } + sealed class EnumeratorPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode { static TaskPool pool; @@ -215,7 +228,7 @@ namespace Cysharp.Threading.Tasks } else { - yield return null; + goto WARN; } } else if (current is IEnumerator e3) @@ -228,9 +241,15 @@ namespace Cysharp.Threading.Tasks } else { - // WaitForEndOfFrame, WaitForFixedUpdate, others. - yield return null; + goto WARN; } + + continue; + + WARN: + // WaitForEndOfFrame, WaitForFixedUpdate, others. + UnityEngine.Debug.LogWarning($"yield {current.GetType().Name} is not supported on await IEnumerator or IEnumerator.ToUniTask(), please use ToUniTask(MonoBehaviour coroutineRunner) instead."); + yield return null; } } @@ -261,5 +280,4 @@ namespace Cysharp.Threading.Tasks } } } -} - +} \ No newline at end of file diff --git a/src/UniTask/Assets/Scenes/SandboxMain.cs b/src/UniTask/Assets/Scenes/SandboxMain.cs index 7fa0bf9..fbbc9a3 100644 --- a/src/UniTask/Assets/Scenes/SandboxMain.cs +++ b/src/UniTask/Assets/Scenes/SandboxMain.cs @@ -507,8 +507,19 @@ public class SandboxMain : MonoBehaviour CancellationTokenSource quitSource = new CancellationTokenSource(); + + IEnumerator TestCor() + { + Debug.Log("start cor"); + yield return null; + yield return new WaitForEndOfFrame(); + Debug.Log("end cor"); + } + async UniTaskVoid Start() { + await TestCor().ToUniTask(this); + Debug.Log("App Start"); Application.quitting += () =>