mirror of https://github.com/Cysharp/UniTask
Fix await IEnumerator + WaitForSeconds does not follow timescale(to behave same as StartCoroutine) #133
parent
b64f31eb0b
commit
bb6dbfa920
|
@ -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;
|
||||
|
|
|
@ -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));
|
||||
|
|
Loading…
Reference in New Issue