When does Unity's LoadScene () function change the scene?

When calling the LoadScene () function, does it immediately switch scenes or simply signals that the scene should be changed? No documentation for LoadScene () specified.

The specific example I'm working with is as follows:

LoadScene(levelName); ItemBox newBox = (ItemBox)Instantiate(...); 

Thus, with the above code, whether the newBox will exist in the newly loaded scene or will it be created in the old scene and then destroyed when loading a new level.

+5
source share
2 answers

it

  UnityEngine.SceneManagement.SceneManager.LoadScene("Gameplay"); 

and yes, he does it “instantly,” that is, s ynchronously.

In other words, it “stops” on this line of code, waits until it loads the entire scene (even if it takes a few seconds), and then a new scene begins.

Do not use the old command specified in the question.

Please note that Unity also has the ability to sync a . She slowly loads a new scene in the background.

However: I recommend you use the usual "LoadScene". All that matters is reliability and simplicity. Users simply don't mind if the car just “stops” for a few seconds while the level is loading.

(Each time I press "Netflix" on my TV, it takes some time for the TV. Nobody cares - that's fine.)

But if you want to download in the background, here's how ...

 public void LaunchGameRunWith(string levelCode, int stars, int diamonds) { .. analytics StartCoroutine(_game( levelCode, superBombs, hearts)); } private IEnumerator _game(string levelFileName, int stars, int diamonds) { // first, add some fake delay so it looks impressive on // ordinary modern machines made after 1960 yield return new WaitForSeconds(1.5f); AsyncOperation ao; ao = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync("Gameplay"); // here exactly how you wait for it to load: while (!ao.isDone) { Debug.Log("loading " +ao.progress.ToString("n2")); yield return null; } // here a confusing issue. in the new scene you have to have // some sort of script that controls things, perhaps "NewLap" NewLap newLap = Object.FindObjectOfType< NewLap >(); Gameplay gameplay = Object.FindObjectOfType<Gameplay>(); // this is precisely how you conceptually pass info from // say your "main menu scene" to "actual gameplay"... newLap.StarLevel = stars; newLap.DiamondTime = diamonds; newLap.ActuallyBeginRunWithLevel(levelFileName); } 

Note: the script answers the question of how you transmit information "from your main menu" when a player clicks "play" on the real game scene. "

+9
source

Maybe that has changed. From the docs:

When using SceneManager.LoadScene, loading does not happen immediately, it ends in the next frame.

I did the same as the poster, and it was an embodiment on the old stage.

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

0
source

Source: https://habr.com/ru/post/1245945/


All Articles