I made the Loader for game based on LoadSceneAsync():
- Initially is loaded the Loader Scene with LoaderManager (LoaderManager) script/GameObject.
- LoaderManager call LoadSceneAsync() for two scenes: Menus and Game.
a) Scenes are loading with AsyncOperation.allowSceneActivation = false - the First scene (“Menu”) will be activated (started) only after all scenes will be loaded and after certain statement - allowSceneActivation set to true (method named as “StartAfterLoadedAllScenes”)
b) Scenes are loading with (default AsyncOperation.allowSceneActivation = true) and every scenes will be activated (started) just after only after it will be loaded automatically (method named as “StartFirstLoadedScene”)
below TL;DR can skip and go to end to summary and to question/recommendation
In Second case all works as predictable, bellow a log load:
Legend:
“LoaderManager (LoaderManager) [1] [Loader] StartFirstLoadedScene() : LoadCycle:”
The scirpt LoaderManager (from GameObject LoaderManager) at Frame #1 from “Loader” Scene
called the Debug.Log(“StartFirstLoadedScene() : LoadCycle”)
“[0] currentOperation=0.9 totalIsDone=False”
The current value AsyncOperation progress = .9 and isDone=false, [0] - Menu and [1] - Game scene
- LoaderManager (LoaderManager) [1] [Loader] StartFirstLoadedScene()
- LoaderManager (LoaderManager) [1] [Loader] StartFirstLoadedScene() : LoadCycle
- [0] currentOperation=0.9 totalIsDone=False
- [1] currentOperation=0 totalIsDone=False
- sum=0.9 45.00%
- LoaderManager (LoaderManager) [2] [Loader] StartFirstLoadedScene() : LoadCycle
- [0] currentOperation=0.9 totalIsDone=False
- [1] currentOperation=0.9 totalIsDone=False
- sum=1.8 90.00%
- MenuSceneManager (MenuSceneManager) [3] [Menus] Awake()
- LoaderManager (LoaderManager) [3] [Loader] StartFirstLoadedScene() : LoadCycle
- [0] currentOperation=1 totalIsDone=True
- [1] currentOperation=0.9 totalIsDone=False
- sum=1.9 95.00%
- LoaderManager (LoaderManager) [4] [Loader] StartFirstLoadedScene() : LoadCycle
- [0] currentOperation=1 totalIsDone=True
- [1] currentOperation=0.9 totalIsDone=False
- sum=1.9 95.00%
- GameSceneManager (GameSceneManager) [5] [Game] Awake()
- Temporary for [Game] Scene was activate [ButtonRestart] GameObject.
- LoaderManager (LoaderManager) [5] [Loader] StartFirstLoadedScene() : LoadCycle
- [0] currentOperation=1 totalIsDone=True
- [1] currentOperation=1 totalIsDone=True
- sum=2 100.00%
- LoaderManager (LoaderManager) [6] [Loader] AllScenesLoadedActivated()
- GameMainManager (GameMainManager) [6] [Loader] AllScenesLoaded()
On step 10 was started the Awake() from Menu Scene
On step 19 was started the Awake() from Game Scene
On step 25 the LoaderManager detected that all scenes loaded and activated (started)
Note. It made one “yield return null” before send “AllScenesLoadedActivated()” (Unity recommendation)
But In Case One (“a)”), bellow a log load:
- LoaderManager (LoaderManager) [1] [Loader] StartAfterLoadedAllScenes()
- LoaderManager (LoaderManager) [1] [Loader] StartAfterLoadedAllScenes() : LoadCycle
- [0] currentOperation=0.003146853 currentOperation=False
- [1] currentOperation=0 currentOperation=False
- sum=0.001573427 0.09%
- LoaderManager (LoaderManager) [2] [Loader] StartAfterLoadedAllScenes() : LoadCycle
- [0] currentOperation=0.9 currentOperation=False
- [1] currentOperation=0.9 currentOperation=False
- sum=1.8 100.00%
- LoaderManager (LoaderManager) [3] [Loader] AllScenesLoadedActivated()
- MenuSceneManager (MenuSceneManager) [4] [Menus] Awake()
- GameSceneManager (GameSceneManager) [6] [Game] Awake()
- GameMainManager (GameMainManager) [7] [Loader] AllScenesLoaded()
Exist a very strange and not clear behaviour:
After step 9 - detection that all scene was loaded and after one “yield return null”, the all loaded Scenes was activated (set allowSceneActivation= true).
On step 10 - LoaderManager scipt filling that all Scenes loaded and activated, but:
Only On step 11 (Frame#4) will be called Awake() from Menus Scene. It’s understandable (next Update after scene activation)
But Only On step 12 (Frame#6) will called Awake() from Game Scene.
It’s a strange, I was awaited that it will in simultaneous with Menu Scene.
For this case I made in LoaderManager() special delay before send the send “AllScenesLoadedActivated()” to GameMainManager.
Summary
In my case the special delay was the four “yield return null” between Awake()
I don’t know Is it the standard delay between finishing of activation Scenes which was fully loaded and after that activated simultaneously? or it depends on of content the Scenes and computer performance.
Simple workaround - not use the simultaneous activation more than one Scene (it’s a have some logic).
I settled on that, that I will use the first variant “a)”