Search Unity

General workflow and best practice question: loading/unloading stuff

Discussion in 'Editor & General Support' started by aivo, Jul 18, 2019.

  1. aivo

    aivo

    Joined:
    Feb 13, 2017
    Posts:
    30
    Hello dear community

    I hope this is the right section: I have a general question about loading/unloading stuff and how unity is handling it in terms of memory and performance.

    I am working on a scene and in there I have a bunch of different modules. Each module contains some geometry and some custom scripts and components. In the hierarchy they are all organized under separate GameObjects. In the editor I wan't to work on all of them, thus they are all activated.

    In the build though, only one of these modules needs to be loaded/activated (ever!), depending on some specific parameters of the device the build runs on. Whats the best approach for this?
    What I currently have, is that the modules which are not needed, are deactivated by calling GameObject.SetActive(false) at the Start() function. I guess I also need to call Object.DestroyImmediate()?
    Would it be better to convert these modules as prefabs and load the required module calling Resources.Load() and then call Resources.UnloadUnusedAssets()?

    Whats are the differences regarding performance and memory useage?

    I hope somebody could shed some light on this.


    best
    aivo
     
  2. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    If GameObjects are inactive, they should not cause any framerate drops at runtime. They do add to the overhead of having to load objects during scene loading and they need to be stored in memory.

    If loading times and memory is a concern for your application, it would be better to remove the objects before the runtime starts and only load what you need. As you stated, that could be achieved by not having any objects in the scene and instead load them as prefabs via Resources.Load(). Mind, that a direct reference (public GameObject field) in one of the MonoBehaviour of the scene could also force Unity to load the prefab, but Resources.Load() will not until you call it. An improvement to using the Resources API would be AssetBundles, but that is a bigger topic with a lot of details. In short, Unity recommends AssetBundles over the Resources folders.

    Additionally, it would be possible to destroy the objects during the build process and only let the one module that should be active for this type of build in the scene. This would be my personal approach, because it sounds like the simplest and fulfills all performance needs, as long as you don't need the destroyed objects dynamically at runtime. For this you would look into callbacks such as OnProcessScene.
     
    aivo likes this.
  3. aivo

    aivo

    Joined:
    Feb 13, 2017
    Posts:
    30
    Thanks for your detailed reply.
    It seems simply destroying the objects actually perfectly suits my needs, since all the objects are already in my scene and cannot have different builds.