Search Unity

Question Pre-loading multiple scenes to avoid loading times

Discussion in 'Scripting' started by PascalTheDog, Jul 18, 2021.

  1. PascalTheDog

    PascalTheDog

    Joined:
    Mar 16, 2015
    Posts:
    86
    Hey,

    In Paper Mario games and many others, a given scene will usually have two or more exits leading to other scenes without the need for loading times. I assume this was achieved by pre-loading all the scenes you can reach from a given location so that by the time the player walks to an exit, all that is needed for the scene loader is to be given the green light — since the heavy duty loading was already done in the background.

    Now, LoadSceneAsync technically allows for something like that: You load a scene on Start and set its allowSceneActivation to false, meaning it won't actually activate until you tell it to (by setting allowSceneActivation to true). The problem is, that only works for one scene; you can't pre-load multiple scenes that way. The documentation insists on that aspect, and when I tried to load multiple scenes anyway Unity flat out crashed. That seems to be a big no-no.

    I'm not sure where to look next for a way to load as many scenes as reasonably possible in the background on Start if the SceneManager can't help. Assistance would greatly appreciated. Cheers!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Nintendo first party games have always been amazing in this no-loading regard.

    It takes a lot of work to reach their level.

    For one, they keep their level data extremely small, so that loading is effectively instantaneous.

    For two, they control ALL the hardware and software, so they're not (generally) using a generic engine like Unity.

    You are almost ALWAYS going to have some type of hiccup, especially if there are big textures that need to come into being at some point in your level.

    About the best you can hope for is to constantly test your levels (on the actual hardware!), optimize your level size to be as small as possible, keep multiple scenes in one single scene, preload portions of your next level in clever ways to hide loading, give the user something interesting to do during loads, etc.
     
  3. PascalTheDog

    PascalTheDog

    Joined:
    Mar 16, 2015
    Posts:
    86
    Thank you for your response.

    The Paper Mario-inspired indie game Bug Fables (which was developed using Unity) did a great job replicating Paper Mario's seamless scene transition. I'm sure it's possible to come up with a solution; it's just that Unity's built-in solution has that weird limitation where you can't pre-load more than one scene at once. My project involves fairly lightweight scenes (both objects- and scripts-wise) that load up in a matter of a couple of seconds, so I'm confident this is more of an issue relating to approach than optimization.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Properly designed scenes can be loaded and turned OFF in their entirety, so assuming you can fit 3 scenes in memory at once:

    - enter scene 1
    - additively load scene 2 (one possible exit) and disable all
    - additively load scene 3 (the other possible exit) and disable all

    The moment you hit the scene 1 exit and need scene 2 or scene 3:

    - activate the scene you want.
    - unload scene 1
    - unload the other not-used scene
    - start additively loading possible exit scenes

    Additive scenes are pretty amazing. Once you start using them, you'll never go back. Here's some more blurbs:

    Additive scene loading is one possible solution:

    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6630961
    https://forum.unity.com/threads/right-way-for-performance-divide-scene.1023673/#post-6754330

    https://forum.unity.com/threads/problem-with-canvas-ui-prefabs.1039075/#post-6726169

    A multi-scene loader thingy:

    https://pastebin.com/Vecczt5Q

    My typical Scene Loader:

    https://gist.github.com/kurtdekker/862da3bc22ee13aff61a7606ece6fdd3

    Other notes on additive scene loading:

    https://forum.unity.com/threads/removing-duplicates-on-load-scene.956568/#post-6233406

    Timing of scene loading:

    https://forum.unity.com/threads/fun...ject-in-the-second-scene.993141/#post-6449718

    Also, if something exists only in one scene, DO NOT MAKE A PREFAB out of it. It's a waste of time and needlessly splits your work between two files, the prefab and the scene, leading to many possible errors and edge cases.

    Checking if everything is ready to go:

    https://forum.unity.com/threads/daily-events-and-content-changes.1108202/#post-7143713
     
  5. PascalTheDog

    PascalTheDog

    Joined:
    Mar 16, 2015
    Posts:
    86
    I had not thought about that! Thanks for the detailed response.
     
    Kurt-Dekker likes this.