How to load a DDOL scene additively on startup ?

Hello,

I’m working on a rather large multi-scenes project. There are quite a few objects that we want to exist as singletons throughout the entire project. To do that, we explored two solutions, but both have disadvantages and we’re stuck trying to come up with a decent solution to this issue. Here’s what we tried so far :

  1. Have an “Init” scene with all our DontDestroyOnLoad (DDOL) stuff. The project always starts with this scene, and it was made so the scene can never be loaded again. When it’s done instantiating everything, it loads another scene. It works just fine, our global objects are present in every other scene, but it’s not very handy : whenever you’re working on another scene, you always have to go back to the Init scene to hit play and test your stuff. Also, we also have to change the 1st scene loaded after Initialization all the time so it loads right after Init, instead of just playing the scene we’re currently working on.

  2. Since we were unhappy about that, we decided to remove the “Init” scene and have an “InitGuard” object in every scene, with top priority in the Script Excecution Order. The InitGuard just checks if the global objets are here, and if not, creates them. It allows us to start the application from any scene, which is great. But since all our DDOL objects are created from code, we can’t have them reference each other directly from the editor, which is annoying, and also leads to execution order issues in some cases.

We figured that a good solution to this would be to have an Init scene with all the DDOL stuff, and an InitGuard object in every other scene that would load the Init scene additively if it hasn’t been done before. This way we’d keep the advantages of both methods. Problem is, we can’t get it to work :slight_smile: Upon loading a scene, the InitGuard checks for the existence of the DDOL stuff in its Awake(). If it’s not there, it has to load the Init scene before all other objects from the new scene are created (most of them need references to Init stuff in their Awake()). In a word, we need to load Init additively in the Awake() and wait until it’s finished. And I can’t find a way to do that, all my attempts have failed so far :(.

Any suggestions ?

Thanks in advance,

Hurricanes.

Thanks for your answers.

@DaveA : I don’t really want to get into “late awake” stuff. it’s a lot of code and makes the init process even more complicated. Maybe I didn’t ask my question very clearly, so let me reformulate :

The core of my problem is : How to load additively a scene A at the beginning (understand Awake()) of a scene B, and have A fully load before B does (i.e all the Awake() methods of A are called before those of B, right after the LoadLevel( A ) was called.) ?

In scene B, I’ve got a script (with top priority in the execution order) that tries to load A in its Awake(). I tried using LoadLevelAdditive() and wait for the result (while loop on Application.isLevelLoaded), but then I get stuck in an infinite loop. I then tried to use LoadLevelAdditiveAsync in a coroutine, but obviously the main execution still continues and the objects of B are awaken before A finishes loading.

I just can’t seem to find a way to have B load A additively and wait for it to be loaded before doing anything else. I’m probably missing something here, I’d be surprised if there was no way of doing this :slight_smile:

PS : do you think i should rename/edit the original post to reflect better the question i’m actually asking ?

If setting the execution order manually (new with 3.4 I believe) isn’t cutting it, worst case, could you check flags in Update? That is, set ‘loaded or not’ kinda flags and the first calls to Update would check these and finish initing whatever is left to do. Then not again. So the first Update might be slow (functioning as a sorta ‘late Awake’ cleanup), but thereafter would not do anything more (to init things that is).

I just figured it out actually :). Instead of trying to load a scene A at the beginning of a scene B, we just put all the contents of scene A into a prefab and we instantiate it during the awake of scene B if it doesn’t exist already. It solves pretty much all the issues we had without losing any functionalities, which is great :slight_smile: