Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Don't miss out on the Unite LA Keynote for the latest information from Unity! Set a reminder!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Persistent Level gameObjects

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

  1. SamohtVII

    SamohtVII

    Joined:
    Jun 30, 2014
    Posts:
    58
    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:
    19
    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. DespairBear

    DespairBear

    Joined:
    Nov 1, 2013
    Posts:
    171
    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. VergilUa

    VergilUa

    Joined:
    Dec 22, 2014
    Posts:
    759
    Another option is to use additive scene loading, and multiscene setup, to avoid neccessary loading / unloading.
     
    Kiwasi likes this.
  5. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    847
    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:
    15,669
    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.
     
    LurkingNinjaDev likes this.
  7. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    847
    I was concentrating on the loading/unloading, but yeah, you're right, of course.
     
    Kiwasi likes this.