Either I am not getting Scene Loading in Unity or I am too dense…
When Initializing my game, I want to preload a bunch of stuff while showing a loading bar and once everything is done, I want to switch to the scene containing the actual gameplay level. To not block myself by initializing sequentially - considering I got I-net calls in there - I built a system where each module has an initialization that gets started in a co-routine and will let the GameManager know via a callback once the initialization is completed.
public int InitializeAllModules()
{
foreach (IInitializable module in _moduleInitializationState.Keys)
{
Debug.Log(string.Format(LocalizationManager.instance.GetTranslationFor("time-init-mod"), Time.time, module));
StartCoroutine(module.Initialize(FinishedInitialization));
// Updating of loading bar and some other bookkeeping omitted
}
return _modulesToLoad;
}
Worked great when setting it up and testing the loading, but now I added the module that is supposed to handle the loading of my next level. So I wrote this Initialize function for that module:
AsyncOperation operation;
public IEnumerator Initialize(Action<IInitializable> endCallback)
{
operation = SceneManager.LoadSceneAsync("GameScene");
operation.allowSceneActivation = false;
operation.priority = 1;
yield return operation;
endCallback(this);
}
Now my problem is that - since I yield for the AsyncOperation and apparently from what I found on the web .allowSceneActivation = false makes the loading process incomplete until the bool is switched - my SceneLoader gets stuck and never reports to my GameManager that it’s done. Thus my GameManager has no clue that it should just tell the SceneLoader to .allowSceneActivation = true - unless I make some roundabout hack that makes an exception to the way the SceneLoader is handled.
Ideally what I would want is:
- SceneLoader preloads the scene
- Unity’s SceneManager knows it has two scenes loaded
- GameManager gets the “all clear” from all modules
- GameManager tells the SceneManager to switch and unload the initial “loading screen” scene…
but it seems that Unity doesn’t support that…???
Or am I missing something somewhere?
Is there a way to make it still work they way I want? Or do I have to find some dirty hack to set the flag whenever everything else is done loading?