UniTask RunOnThreadPool background
DevFearScar opened this issue · 1 comments
I noticed a distinctive feature. If you run the android Task application while minimizing.Run, then it will work even if the game is paused. However, the alternative code using UniTask is always frozen until the player returns to the game.
public class Test : MonoBehaviour
{
private void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
Task.Run(Run);
}
private async Task Run()
{
var counter = 0;
while (counter < 100)
{
counter++;
Debug.Log(counter);
await Task.Yield();
}
}
}
If u use await Task.Yield();
thay return the state machine to the next execution state and this can be done using UniTask.Delay
If you use UniTask.Delay
to wait a certain amount of time, it will be like a standard Task.Delay
This method is frame-agnostic, so it will continue even if the game is minimized, as long as the operating system allows the code to run in the background.
async UniTask MyAsyncMethod()
{
// This will work even if the game is minimized
await UniTask.Delay(1000);
// Other code
}
so in case with UniTask.Yield()
it will be frozen
async UniTask MyFrameDependentMethod()
{
// This will pause when the game is minimized
await UniTask.Yield(PlayerLoopTiming.Update); // or await UniTask.Yield();
// Other code
}
Also, if you look at the source code for the Yield function, you will see that it works in some context of the life cycle of the unit, for example Update
FixedUpdate
LateUpdate
etc.