Fix await IEnumerator + WaitForSeconds does not follow timescale(to behave same as StartCoroutine) #133

master
neuecc 2020-08-14 17:29:30 +09:00
parent b64f31eb0b
commit bb6dbfa920
2 changed files with 29 additions and 2 deletions

View File

@ -225,12 +225,12 @@ namespace Cysharp.Threading.Tasks
static IEnumerator UnwrapWaitForSeconds(WaitForSeconds waitForSeconds) static IEnumerator UnwrapWaitForSeconds(WaitForSeconds waitForSeconds)
{ {
var second = (float)waitForSeconds_Seconds.GetValue(waitForSeconds); var second = (float)waitForSeconds_Seconds.GetValue(waitForSeconds);
var startTime = DateTimeOffset.UtcNow; var elapsed = 0.0f;
while (true) while (true)
{ {
yield return null; yield return null;
var elapsed = (DateTimeOffset.UtcNow - startTime).TotalSeconds; elapsed += Time.deltaTime;
if (elapsed >= second) if (elapsed >= second)
{ {
break; break;

View File

@ -96,6 +96,33 @@ namespace Cysharp.Threading.TasksTests
// l[1].Item2.Should().NotBe(currentFrame); // 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) IEnumerator Worker(List<(int, int)> l)
{ {
l.Add((0, Time.frameCount)); l.Add((0, Time.frameCount));