DEV Community

Discussion on: Advanced scenes management in Unity

Collapse
krummja profile image
Jonathan Crum • Edited on

So I just ran across this while researching solutions for scene management in code, and I have to say this is really slick! I actually want to offer a small addition that I find extremely handy:

In the OpenSceneWithArgs method of SceneManager, add a parameter bool additive and the following to the method body:

LoadSceneMode mode = additive ? LoadSceneMode.Additive : LoadSceneMode.Single;
return UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneName, mode);
Enter fullscreen mode Exit fullscreen mode

Then, in any given SceneController subclass, you can call subscenes, leaving the main scene to be a master controller and additively loading additional scenes as needed:

public sealed class MainMenuArgs : SceneArgs
{
    TestSceneArgs testSceneArgs = new TestSceneArgs();
    SceneManager.OpenSceneWithArgs<TestSceneController, TestSceneArgs>(testSceneArgs, true);
}
Enter fullscreen mode Exit fullscreen mode

Inside my MainMenuController I of course call Args.SomeInitializer() and boom, subscene loaded up.