Looks like UniTask.Yield(PlayerLoopTiming.Update) is not resuming at Update sometimes #561
-
Hello, I'm using Unity 2023.2.11f1 and UniTask 2.5.4. SummaryTo be short, it looks to me that await UniTask.Yield(PlayerLoopTiming.Update, cancellationToken: cancel); is not resuming the execution flow at Update sometimes and the behaviour is not repeatable. ProcedureThat is, I started from a Standard (URP) scene, created an empty GameObject, added MyUniTaskTest component as below.. using Cysharp.Threading.Tasks;
using System.Threading;
using UnityEngine;
public class MyUniTaskTest : MonoBehaviour
{
string _phase;
bool _done;
void OnEnable()
{
_done = false;
}
void FixedUpdate()
{
_phase = "FixedUpdate_Begin";
_phase = "FixedUpdate_End";
}
void Update()
{
_phase = "Update_Begin";
if (!_done)
{
_done = true;
MyTaskAsync(destroyCancellationToken).Forget();
}
_phase = "Update_End";
}
void LateUpdate()
{
_phase = "LateUpdate_Begin";
_phase = "LateUpdate_End";
}
async UniTaskVoid MyTaskAsync(CancellationToken cancel)
{
print($"MyTaskAsync");
for (int i = 0; i<30; i++)
{
print($"{i}: {Time.frameCount} {_phase}");
await UniTask.Yield(PlayerLoopTiming.Update, cancellationToken: cancel);
}
}
} Run the scene in Play mode. ResultsWhat's printed in the Console was as follows.
ExpectedIf the task is resumed at the timing of Update, which is after FixedUpdate and before LateUpdate, the output would be either FixedUpdate_End, Update_Begin, or Update_End. Why is LateUpdate_End printed sometimes? Correct me if I am using the API wrongly or I I misunderstand something. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Beta Was this translation helpful? Give feedback.
FixedUpdate
is not guaranteed to run every frame.PlayerLoopTiming.Update
runs before your MonoBehaviour'sUpdate
in the frame. Therefore, the only things it could ever be areUpdate_Begin
FixedUpdate_End
orLateUpdate_End
, becauseFixedUpdate_End
orLateUpdate_End
were the last things to be updated on the previous frame.