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

UniTask runs one frame even after exit PlayMode. #543

Open
OkdolLab opened this issue Feb 13, 2024 · 1 comment
Open

UniTask runs one frame even after exit PlayMode. #543

OkdolLab opened this issue Feb 13, 2024 · 1 comment

Comments

@OkdolLab
Copy link

public class TestA : MonoBehaviour
{
	async UniTask OnEnable() {
		_cts = new CancellationTokenSource();
		try {
			print("try [frameCount:" + Time.frameCount + "]" + "[isPlaying:" + Application.isPlaying + "]");
			await UniTask.Delay(10000, cancellationToken: _cts.Token);
		}
		catch (Exception e) {
			print("catch [frameCount:" + Time.frameCount + "]" + "[isPlaying:" + Application.isPlaying + "]" + "\n" + e);
		}
		finally {
			print("finally [frameCount:" + Time.frameCount + "]" + "[isPlaying:" + Application.isPlaying + "]");
			GameObject gameObject = new GameObject();
		}
	}
	void OnDisable() {
		print("OnDisable, Cancel [frameCount:" + Time.frameCount + "]" + "[isPlaying:" + Application.isPlaying + "]");
		_cts.Cancel();
	}

	CancellationTokenSource _cts;
}

When you quit immediately after playing with this code, a game object is created in the scene.

image

In my short opinion, Play Mode should not affect Edit Mode. but..

Is this behavior as intended?

@neuecc
Copy link
Member

neuecc commented Feb 14, 2024

The additional slight rotation at the end mimics the UnitySynchronizationContext.
https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Scripting/UnitySynchronizationContext.cs#L117
Whether the current UniTask behavior is really appropriate is debatable.

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