Additive Scenes

Last modified by Maarten Struijk Wilbrink on 2021/11/25 16:46

As explained here, it is good practice to use additive scene loading in your program. This allows you to create one (or multiple) base scenes, which can each hold a specific section of code, GameObjects, and data. Scenes can be loaded additively on top of each other, 

To do so, you have to actively set the LoadSceneMode to Additive:
SceneManager.LoadScene("YourScene", LoadSceneMode.Additive);

An even better way to do this, is to load your scenes asynchronously, so that loading the extra scene does not delay the running of the rest of the program
private void LoadScene()
{
   StartCoroutine(LoadYourAsyncScene("YourScene"));
}

private IEnumerator LoadYourAsyncScene(string sceneName)
{
    AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);

   while (!asyncLoad.isDone) // Wait until the asynchronous scene fully loads
   {
      yield return null;
    }
}

If you want to unload your scene, you can use
private void UnLoadScene()
{
   StartCoroutine(UnLoadYourAsyncScene("YourScene"));
}

private IEnumerator UnLoadYourAsyncScene(string sceneName)
{
    AsyncOperation asyncUnLoad = SceneManager.UnLoadSceneAsync(sceneName);

   while (!asyncUnLoad.isDone) // Wait until the asynchronous scene fully loads
   {
      yield return null;
    }
}

Addive Sceneloading in Photon

Unfortunately, the Multiplayer package Photon does not allow for Additive scene loading out of the box. A custom additive scene loading solution can be made, however, in this case it may be best to use standard scene loading. Consider using DontDestroyOnLoad instead.

Tags:
   
solo
XWiki 14.10.13
contact@xwiki.com