Add enumerator.ToUniTask(MonoBehaviour coroutineRunner), log WARN on await enumerator when yield not supported types(Coroutine, WaitForEndOfFrame, WaitForFixedUpdate).

master
neuecc 2020-09-22 10:08:03 +09:00
parent 8537ddf8a6
commit 37e8b4500e
2 changed files with 34 additions and 5 deletions

View File

@ -32,6 +32,19 @@ namespace Cysharp.Threading.Tasks
return new UniTask(EnumeratorPromise.Create(enumerator, timing, cancellationToken, out var token), token); 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<EnumeratorPromise> sealed class EnumeratorPromise : IUniTaskSource, IPlayerLoopItem, ITaskPoolNode<EnumeratorPromise>
{ {
static TaskPool<EnumeratorPromise> pool; static TaskPool<EnumeratorPromise> pool;
@ -215,7 +228,7 @@ namespace Cysharp.Threading.Tasks
} }
else else
{ {
yield return null; goto WARN;
} }
} }
else if (current is IEnumerator e3) else if (current is IEnumerator e3)
@ -228,9 +241,15 @@ namespace Cysharp.Threading.Tasks
} }
else else
{ {
// WaitForEndOfFrame, WaitForFixedUpdate, others. goto WARN;
yield return null;
} }
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;
} }
} }
@ -262,4 +281,3 @@ namespace Cysharp.Threading.Tasks
} }
} }
} }

View File

@ -507,8 +507,19 @@ public class SandboxMain : MonoBehaviour
CancellationTokenSource quitSource = new CancellationTokenSource(); CancellationTokenSource quitSource = new CancellationTokenSource();
IEnumerator TestCor()
{
Debug.Log("start cor");
yield return null;
yield return new WaitForEndOfFrame();
Debug.Log("end cor");
}
async UniTaskVoid Start() async UniTaskVoid Start()
{ {
await TestCor().ToUniTask(this);
Debug.Log("App Start"); Debug.Log("App Start");
Application.quitting += () => Application.quitting += () =>