Search Unity

Persistent Level gameObjects

Discussion in 'General Discussion' started by SamohtVII, Oct 12, 2018.

  1. SamohtVII

    SamohtVII

    Joined:
    Jun 30, 2014
    Posts:
    370
    Does Unity have anything in place where if I have a large scene with x amount of objects, then load a new scene with the same objects (But arranged differently) it will load quicker?

    Or does it just dump everything and rebuild the new level from scratch?
     
  2. hjohnsen

    hjohnsen

    Joined:
    Feb 4, 2018
    Posts:
    67
    Interesting question, I don't know if anybody already tried to benchmark Unity scene loading.
    If you want to handle it by yourself then you can probably set the 'don't destroy' flag before the new scene loading and then place the persistent objects at their new location.
    hj.
     
  3. lo-94

    lo-94

    Joined:
    Nov 1, 2013
    Posts:
    282
    It just loads in the new level from scratch as Unity has no way of knowing if those references are to the exact same object between scenes, as scenes store references to each gameobject individually. If you want persistence then you'd have to dynamically load. Achieving what you want would be pretty simple. Essentially you'd just need to set them not to destroy on load, and have an array of the positions that you want them set to, and on load set them to the appropriate position.

    That's the easiest way I can think of at least. It's obviously going to load things differently between scenes because scenes each have their own references to gameobjects.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Another option is to use additive scene loading, and multiscene setup, to avoid neccessary loading / unloading.
     
    Kiwasi likes this.
  5. Well, I think there is a misconception here.

    If you're asking that does unity reload all the assets between scenes, the answer is: no.
    Asset load is not dependent on the scene load other than the fact that unity is loading the assets which is not loaded already.
    So in a sense, unity does what you're asking in some way.

    If you have a finite set of assets which is used in the scenes, than unity will never unload the assets themselves, it will 'rearrange' them to cover the new scene. The thing which will be loaded is the scene asset.

    https://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html
    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.UnloadScene.html
    At least this is my interpretation what's happening.
     
    Kiwasi and Joe-Censored like this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's half the story. That covers assets like sound tracks, models or prefabs.

    But the actual scene GameObjects get removed and new ones created in single scene loading. Which can take a non trivial amount of loading time if you have heavy scripts involved.

    You can get around this with additive scene loading or object pooling as appropriate for you game. If it needs it, many games don't.
     
    Lurking-Ninja likes this.
  7. I was concentrating on the loading/unloading, but yeah, you're right, of course.
     
    Kiwasi likes this.