Search Unity

Anyone use multi-scene in their project?

Discussion in 'General Discussion' started by Rechronicle, Feb 24, 2022.

  1. Rechronicle

    Rechronicle

    Joined:
    Dec 14, 2017
    Posts:
    32
    Hi!
    Does anyone use multi-scene? How do you handle data between scenes?
    Right now I'm using serialize & deserialize along with ScriptableObject to solve it. Would really interesting to know other options that I can try.

    Tips on managing multi-scene would also be helpful. I like that I can working on any scene without worrying about interfering with other systems.

    Thanks!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,934
    I absolutely use multi-scenes. Would imagine it'd be hard to make many games without it. Seperate levels are of course their own scenes, the player is their own scene, the general UI (Hud that's always present) is its own scene, and other major UI layouts are their own scene too. Pretty easy to work with when using addressables too.

    What do you mean by 'serialize & deserialize'? Do you mean writing to and reading from scriptable objects public fields/properties? Or something else?

    I certainly use scriptable objects for trans-scene communication for the purpose of communicating information. Scene transitions is one example.

    But when I just need something in one scene to tell something in another scene to do something, I generally use a Publisher-Subscriber (or Event Broker) pattern. Singleton is another option, though I like to keep the scope of what can be accessed very small hence using the pub-sub pattern.
     
    Rechronicle likes this.
  3. kittik

    kittik

    Joined:
    Mar 6, 2015
    Posts:
    565
    Using multiple scenes is a really good way to separate logic, that does not need to be together. A Player, Game Logic etc. does not need to be part of a scene of environmental assets.

    What do you want to serialize/deserialize? Context required.
     
    Rechronicle likes this.
  4. Rechronicle

    Rechronicle

    Joined:
    Dec 14, 2017
    Posts:
    32
    For context,
    there are 3 scenes in my current game (explore-battle-menu, all loaded per single scene), all of these scenes need to know about the progress of the player (health, level progress, etc). I'm using ES3 (EasySave3) to help with the serialization, so I set it up at the awake of each scene solely for bringing the data to other scenes.
    It's working but occasionally the data has not loaded before other systems try to pull it.

    I'm starting to think maybe there should be a scene that is always active to contain so they can be ready whenever each system needs it. But then everything will depend on that scene, much like Singleton. Either way, going to try some more and find what fits best for my project. Maybe adding a delay at the start of each scene to wait for the data to get loaded.

    Also going to check Pub-Sub & scriptableobject for trans-scene communication, right now I'm only using SO for initialization purposes. Looks like I can use it more than that?


    P.s no hates on Singleton, it just helps to ease my mind working on each scene without tight dependency.
    P.ss sorry it's a long post xd
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,934
    Nothing wrong with a ever-present scene that keeps important data referenced.

    I use a game manager scene for such a purpose, and scriptable objects for all my save data. When the game is loaded, each scripable object gets fed its needed data (I have a seperate SO for each mechanic/system); all of these are referenced in the manager scene so the data remains persistant. This scene is always loaded at the start of the game or when entering play mode.

    Then anything else that needs the data can just reference the SO's. I also set up static methods of retrieving the data as well for anything that can't hold a serialized reference.

    If you're using addressables or asset bundles, you'll need to make sure both the SO's and anything referencing them are either addressable or bundled, otherwise duplicates will be created at build time and you'll encounter bugs in build.
     
    Rechronicle likes this.
  6. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,079
    You can use a scriptable object for more than that but I'm not sure it's a good way to go about things like this as scriptable object's lifetime and event execution can change depending on context. But as long as both scenes directly reference the SO in some inspector, the SO won't be unloaded from memory between scene loads. It gets more complicated with addressables.
     
    Rechronicle likes this.
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    That's how I do it in my project. My game is like a point-and-click adventure game. I have one scene called "main" that has the player, UI, inventory and all the game state data. The locations and interact-able objects come and go, but the "main" scene is always loaded as long as you're playing.
     
    Rechronicle likes this.