yymmyb
yymmyb

Reputation: 104

What is the difference between using a coroutine or not in SceneManager.LoadSceneAsync?

The official example put SceneManager.LoadSceneAsync in the coroutine

But it also works well when I call it directly.

Why the official example call it in the coroutine? What is the difference?

Upvotes: 1

Views: 1004

Answers (2)

aalmigthy
aalmigthy

Reputation: 1311

There is no particular reason other than following the good practice of encapsulation.

My guess would be that the Unity-Engineer himself used the loading in a coroutine and just used that snippet for the documentation. I think he knew a lot of people would start with the snippet and go from there. Resulting in a lot of people who now have encapsulated scene-loading. I like that he cared for this.

Upvotes: 0

Dave
Dave

Reputation: 2862

When you put it in the coroutine you are able to easily check the AsyncOperation state. SceneManager.LoadSceneAsync will load the scene anyway but if you want to check if the scene loaded completely or in what percent (and this is the main reason you are calling LoadSceneAsync) you need to check the AsyncOperation state.

You could check it in the Update method but it makes more sense to use a coroutine so you only check it when you load the scene. You don't need or want to check if the scene has loaded in Update every frame but only in the beginning when the scene loads.

Upvotes: 1

Related Questions