Search Unity

Scene for Each Level?

Discussion in 'Getting Started' started by bcgreen24, May 31, 2016.

  1. bcgreen24

    bcgreen24

    Joined:
    May 28, 2016
    Posts:
    9
    Hi, all-- I'm trying to figure out the best way to implement 'levels' in a game I'm working on.

    So let's say, as an example, you're workin' on a puzzle game of some sort where from level to level the game board changes, but everything else (environment, camera, lights, etc.) stay the same. I'm assuming that rather than create a scene for each level, you'd just reuse the same scene and re-initialize the game board and 're-start' the scene?

    And also, for future reference, if you did create a scene for each level in a game where only a few things were changing from level to level: if you duplicate a scene and just change the elements that are different between the two scenes, will Unity basically re-use the assets that are 'shared' between scenes?

    Thanks!
    Bryan
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I've used a couple of approaches.
    • Create a scene for each level. Make the UI and background a prefab. Any updates to the prefab will update across all levels
    • Build the UI and background in a preloader scene. Call DontDestroyOnLoad so the stuff stays persistent as you change scenes
    • Build everything in one scene. Manually control loading and destroying objects
    Assets are reused. However Unity will remove the specific GameObjects from memory and create new ones.
     
    BadgerScout and Iron-Warrior like this.
  3. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    I just wanted to highlight this as one of the "best" ways of doing it in Unity. A big advantage of this is that the preloader scene is essentially a "prefab" object that can contain other prefabs. So if you have several UI screens in different scenes but they all share similar elements (prefabbed buttons, widgets, etc), then you can just modify one and all of them will be updated. Nested prefabs like this are not natively supported in Unity but having a preloader scene works extremely well for this and is easy to implement.
     
    Eralven, pirate-hx and BadgerScout like this.
  4. knobby67

    knobby67

    Joined:
    Aug 30, 2015
    Posts:
    389
    Can I just ask in relation to this, I'm thinking about same things as op, when I call don't destroy on on a scene and swap to another, does the "background scene" stop running? EG if I have 5 scenes paused for example it's not taking up resources apart from RAM GPU video RAM? So frame rates stay constant?
     
  5. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    There's no such thing as a "background scene"; a "scene" is really just a bundle of objects loaded together, so loading a new scene is only "swapping" scenes in the sense that normally everything from the previous scene is unloaded.

    Anything loaded is actively being used and is in RAM (indeed, that's what loaded means). You can choose to SetActive(false) on the objects you want hidden, but a) that's on you to do, and b) if you're going to do that then I don't understand why you didn't just let them get flushed like normal.
     
    Last edited: Jun 2, 2016
    Eralven likes this.