Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't exit await when stopping Unity Editor #588

Open
nyancodev opened this issue May 30, 2024 · 3 comments
Open

Can't exit await when stopping Unity Editor #588

nyancodev opened this issue May 30, 2024 · 3 comments

Comments

@nyancodev
Copy link

Unity: 2022.3.21f1
UniTask: 2.5.4

If you stop the Unity Editor with this code, it will not advance to wait3
If you destroy GameObject, proceed to wait3
Is this behavior due to the fact that PlayerLoop must be stopped in the Unity Editor?

public class Sample : MonoBehaviour
{
    public async void OnDestroy()
    {
        await UniTask.WaitUntil(() => true);
        Debug.Log("wait1");
        await UniTask.WaitUntil(() => true);
        Debug.Log("wait2");
        await UniTask.WaitUntil(() => true);
        Debug.Log("wait3");
    }
}
@nyancodev nyancodev changed the title Can't exit await when stopping Unity editor Can't exit await when stopping Unity Editor May 30, 2024
@Monsoonexe
Copy link

The problem here is that you're using async void, which uses System.Threading.Tasks.Task (instead of UniTask), and it uses the default SynchronizationContext (which doesn't care about enter/exit playmode at all).

I recommend changing the code to something like this instead
`
private void OnDestroy()
{
// kick off async method
OnDestroyAsync().Forget();
}

private async UniTaskVoid OnDestroyAsync()
{
// ... your async code here
}
`

@nyancodev
Copy link
Author

I tried the code I was presented, but it didn't work
After all there is await that does not proceed when the editor is stopped

@Monsoonexe
Copy link

Monsoonexe commented Oct 21, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants