Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Multiple scenes

Discussion in 'General Discussion' started by DeX24, Feb 13, 2019.

  1. DeX24

    DeX24

    Joined:
    Feb 13, 2018
    Posts:
    7
    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
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    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.

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

    Code (csharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class Example : MonoBehaviour
    6. {
    7.     void Update()
    8.     {
    9.         // Press the space key to start coroutine
    10.         if (Input.GetKeyDown(KeyCode.Space))
    11.         {
    12.             // Use a coroutine to load the Scene in the background
    13.             StartCoroutine(LoadYourAsyncScene("SceneA"));
    14.             StartCoroutine(LoadYourAsyncScene("SceneB"));
    15.             StartCoroutine(LoadYourAsyncScene("SceneC"));
    16.             StartCoroutine(LoadYourAsyncScene("SceneD"));
    17.         }
    18.     }
    19.  
    20.     IEnumerator LoadYourAsyncScene(string sceneName)
    21.     {
    22.         // The Application loads the Scene in the background as the current Scene runs.
    23.         // This is particularly good for creating loading screens.
    24.         // You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
    25.         // a sceneBuildIndex of 1 as shown in Build Settings.
    26.  
    27.         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
    28.  
    29.         // Wait until the asynchronous scene fully loads
    30.         while (!asyncLoad.isDone)
    31.         {
    32.             yield return null;
    33.         }
    34.     }
    35. }
     
    FlightOfOne and DotusX like this.
  4. DeX24

    DeX24

    Joined:
    Feb 13, 2018
    Posts:
    7
    Thanks !!!
     
  5. Mitch_Heinsenberg

    Mitch_Heinsenberg

    Joined:
    Sep 29, 2020
    Posts:
    15
    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!
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    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.
     
    angrypenguin likes this.
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    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.

    upload_2021-5-2_11-52-16.png
     
  8. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    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.
     
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    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.
     
  10. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,321
    Nothing, but it is still several operations instead of one.

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

    Mitch_Heinsenberg

    Joined:
    Sep 29, 2020
    Posts:
    15
    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.
     
  12. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    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.
     
    Mitch_Heinsenberg likes this.
  13. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,967
    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.)
     
  14. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    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.
     
  15. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    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.
     
    Last edited: May 3, 2021
  16. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,891
    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.
     
  17. Mitch_Heinsenberg

    Mitch_Heinsenberg

    Joined:
    Sep 29, 2020
    Posts:
    15
    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.
     
    Last edited: May 3, 2021
    nasos_333 likes this.
  18. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    Using scriptable objects as the glue between them would be one solution to that. Relevant video in the spoiler.

     
  19. nasos_333

    nasos_333

    Joined:
    Feb 13, 2013
    Posts:
    12,891
    Indeed is ond way of handling passing between scenes.