Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Looks like UniTask.Yield(PlayerLoopTiming.Update) is not resuming at Update sometimes #561

Answered by timcassell
rage2050a asked this question in Q&A
Discussion options

Hello,

I'm using Unity 2023年2月11日f1 and UniTask 2.5.4.
Here's what I found (Japanese). https://qiita.com/NekoBeko2024/items/51e57efc60436568dd73
The description below is same as what in the URL above.

Summary

To 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.

Procedure

That 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.

Results

What's printed in the Console was as follows.

i 1st run 2nd run 3rd run
0 Update_Begin Update_Begin Update_Begin
1 FixedUpdate_End FixedUpdate_End FixedUpdate_End
2 FixedUpdate_End FixedUpdate_End FixedUpdate_End
3 FixedUpdate_End FixedUpdate_End FixedUpdate_End
4 FixedUpdate_End LateUpdate_End LateUpdate_End
5 FixedUpdate_End LateUpdate_End LateUpdate_End
6 LateUpdate_End LateUpdate_End FixedUpdate_End
7 FixedUpdate_End FixedUpdate_End LateUpdate_End
8 LateUpdate_End LateUpdate_End LateUpdate_End
9 LateUpdate_End FixedUpdate_End LateUpdate_End

Expected

If 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.

You must be logged in to vote

FixedUpdate is not guaranteed to run every frame. PlayerLoopTiming.Update runs before your MonoBehaviour's Update in the frame. Therefore, the only things it could ever be are Update_Begin FixedUpdate_End or LateUpdate_End, because FixedUpdate_End or LateUpdate_End were the last things to be updated on the previous frame.

Replies: 1 comment 1 reply

Comment options

FixedUpdate is not guaranteed to run every frame. PlayerLoopTiming.Update runs before your MonoBehaviour's Update in the frame. Therefore, the only things it could ever be are Update_Begin FixedUpdate_End or LateUpdate_End, because FixedUpdate_End or LateUpdate_End were the last things to be updated on the previous frame.

You must be logged in to vote
1 reply
Comment options

PlayerLoopTiming.Update runs before your MonoBehaviour's Update in the frame.

Aha, then it makes sense. Thank you!

Because the test project runs with Time / Fixed Timestamp = 0.02, Quality / VSync Count = Every V Blank, and my display has 120Hz frequency, FixedUpdate is called less frequently than Update.

Suppose at the n-th frame ended. _phase = "LateUpdate_End";
Then starts (n+1)-th frame without FixedUpdate. UniTask runs before MyUniTaskTest.Update. So I see LateUpdate_End.

Answer selected by rage2050a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /