Multiple scenes

hi !

can someone tell me if it’s possible to load multiple scenes by clicking 1 button in unity?

for example when i click a button,4 scenes are loaded at the same time,thanks

Not “the same time”. One by one, but additionally and async yes.

See https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

Below is an altered version of the example script for LoadSceneAsync. I don’t see any stated reason in the documentation why you couldn’t load them all at the same time.

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Example : MonoBehaviour
{
    void Update()
    {
        // Press the space key to start coroutine
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Use a coroutine to load the Scene in the background
            StartCoroutine(LoadYourAsyncScene("SceneA"));
            StartCoroutine(LoadYourAsyncScene("SceneB"));
            StartCoroutine(LoadYourAsyncScene("SceneC"));
            StartCoroutine(LoadYourAsyncScene("SceneD"));
        }
    }

    IEnumerator LoadYourAsyncScene(string sceneName)
    {
        // The Application loads the Scene in the background as the current Scene runs.
        // This is particularly good for creating loading screens.
        // You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
        // a sceneBuildIndex of 1 as shown in Build Settings.

        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);

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

Thanks !!!

I apologize in advance for resurrecting a 2 year old thread.

I set up my latest demo to have multiple scenes loading additively (static objects first, then interactive objects, player character, sound, UI, and NPCs, will all go their separate scenes).
This makes scene organization much easier.
Are there good reasons to not be doing it this way?

Also, I have recently been learning about the “dangers” associated with using Coroutines (that they throw exceptions that may crash the game?)
Do you have any best practices when using coroutines for scene management?

Thank you!

Yes.

If you make a change in the static scene, hunting for things you have to fix in other scenes might become problematic.

For example, if you have a city in the static scene, and MOVE the whole city to another faraway location, all the additive loaded npcs and sounds will remain where they were and you’ll need to fix that.

1 Like

Both of these would only truly be a problem if you were working with the scenes one at a time but nothing prevents you from having both scenes open in the editor at the same time and manipulating their objects at the same time.

Just as an example I can drag a box over the cube and the sphere in the screenshot below and it will select both of them. From there I can manipulate them together.

Working this way is as simple as dragging additional scenes into the Hierarchy panel.

Screenshot

7099876--846184--upload_2021-5-2_11-52-16.jpg

In your screenshot, the scenes form different hierarchies.

In my opinion, in case of moveable city a decent idea would be to have a “city root” transform to which npc starting positions and sounds would be parented. Meaning, you move the city by its root, and that moves everything in it.

That does not seem to be possible in case of multiple scenes being open simultaneously.

What’s preventing you from having a root node in each scene? I’m not saying my approach is flawless but your reasons against it are all preventable by simply being careful.

That said there are alternatives available and one of them is to generate separate scenes at build time. You could have an editor script move all the static objects into one scene and all the dynamic objects into a separate scene.

Nothing, but it is still several operations instead of one.

It feels that additive scene loading blurs distinction between scene and prefab.

That is a very good point!
It takes only a second to move objects from both scenes into one, and then move them together.

From many perspectives, a scene is not very different from a prefab.

In my opinion, loading several scenes at once makes organization so much easier.

Meanwhile it only takes that same second to select both of them and move them together.

One major difference is performance in the editor. Modifying a large scene is much quicker than it is to modify a large prefab and even moreso when the prefab in question is made up of multiple nested prefabs.

1 Like

If I recall correctly, there is no real difference from a tech standpoint, a scene is just a prefab with a few more options. There was a post somewhere by a unity staff member explaining this and there was underlying concept of ‘stage’ that would eventually be exposed. I’ll see if can dig it up. (Though, it was a while ago, things may have changed.)

2 Likes

Not if you’re doing it in the Inspector, unless they always happen to have a synchronised parent transform.

And a larger number of opportunities to make mistakes.

My current project loads multiple scenes together, both at runtime and in the Editor. However, scenes are strictly split up in ways that reduce the amount of work developers need to do. In our case it broadly forms a geographical heirarchy (world → region → local area), and it’s pretty rare for a task to require manual work at more than one of those levels (ie: in more than one scene) at the same time.

They’re no more “dangerous” than any other code which can also have errors which kill your game.

My advice would be to work out your scene architecture first, and then figure out which coding constructs are best used to implement it. Note that I’d consider this to be an intermediate or advanced programming task in general. This isn’t because the code is hard, it’s because there are multiple interacting systems you need to understand to get reliable results.

I was working on the assumption that I wouldn’t have to spell that out for people who are already treating multiple scenes as a single composite scene. Though I suppose I should have mentioned it for people like the OP who may not necessarily come to that conclusion.

Only major thing I can think of is object or script references needed from other scenes in another scene’s scripts. That could be a problem I suppose if something like that is needed.

Btw, what is the perk of using multiple scenes, than parent each scene objects in one scene, each parented in a separate gameobject (e.g. names as “Scene 1: Player”, “Scene 2: UI” and so on) and enable-disable these parent objects as needed, this way can have all in one and still be able to preview-edit them separately or in combination.

The only benefit is that multiple scenes make it much easier to keep organized.
You can parent things to objects, or you can just drag the scene you need and work on it. And when you are done, get rid of it

  1. For example, you can have “palette” scenes, for your props and buildings. You drop the scene in the hierarchy, copy/ drag and drop what you need, and then remove the palette.

  2. It makes things modular. You keep the character / controller in a separate scene. You don’t need to edit the scene with all your static objects, to work on your character controller. You don’t need to enable/disable everything else.

1 Like

Using scriptable objects as the glue between them would be one solution to that. Relevant video in the spoiler.

Unite Austin 2017 - Game Architecture with Scriptable Objects

Indeed is ond way of handling passing between scenes.