diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/EnumeratorAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/EnumeratorAsyncExtensions.cs index 4bd7f6d..36dca5a 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/EnumeratorAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/EnumeratorAsyncExtensions.cs @@ -225,12 +225,12 @@ namespace Cysharp.Threading.Tasks static IEnumerator UnwrapWaitForSeconds(WaitForSeconds waitForSeconds) { var second = (float)waitForSeconds_Seconds.GetValue(waitForSeconds); - var startTime = DateTimeOffset.UtcNow; + var elapsed = 0.0f; while (true) { yield return null; - var elapsed = (DateTimeOffset.UtcNow - startTime).TotalSeconds; + elapsed += Time.deltaTime; if (elapsed >= second) { break; diff --git a/src/UniTask/Assets/Tests/CoroutineToUniTaskTest.cs b/src/UniTask/Assets/Tests/CoroutineToUniTaskTest.cs index de50ba4..6f48185 100644 --- a/src/UniTask/Assets/Tests/CoroutineToUniTaskTest.cs +++ b/src/UniTask/Assets/Tests/CoroutineToUniTaskTest.cs @@ -96,6 +96,33 @@ namespace Cysharp.Threading.TasksTests // l[1].Item2.Should().NotBe(currentFrame); //} + [UnityTest] + public IEnumerator WaitForSecondsTest() => UniTask.ToCoroutine(async () => + { + await UniTask.Yield(PlayerLoopTiming.PostLateUpdate); + + Time.timeScale = 0.5f; + try + { + var now = DateTimeOffset.UtcNow; + + await WaitFor(); + + var elapsed = DateTimeOffset.UtcNow - now; + + (5.8f <= elapsed.TotalSeconds && elapsed.TotalSeconds <= 6.2f).Should().BeTrue(); + } + finally + { + Time.timeScale = 1.0f; + } + }); + + IEnumerator WaitFor() + { + yield return new WaitForSeconds(3.0f); + } + IEnumerator Worker(List<(int, int)> l) { l.Add((0, Time.frameCount));