Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Do resources referenced in the Resources folder get loaded recursively on app start?

Discussion in 'Asset Bundles' started by LazloBonin, Aug 24, 2021.

  1. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    809
    Hi! I'm trying to evaluate whether I should use asset bundles for performance.

    I have a system where an "index" ScriptableObject refers to all the prefabs I will instantiate during runtime. This index lives under Resources/Index.asset.

    In a very simplified way, what it does is:
    Code (CSharp):
    1. class Index : ScriptableObject
    2. {
    3.     public GameObject myPrefab;
    4. }
    I'm wondering if this is bad practice in terms of memory pressure and app load.

    Specifically, I'm wondering if, on app load, Unity will traverse my references and deserialize the prefab asset (or even worse, its own references) just because it's referencable from the resources folder, or because I loaded the Index object.

    Of course if I had only 1 prefab reference I wouldn't worry, but in my architecture I have hundreds if not thousands, hence the question.

    To be clear, all my prefabs are for the "base game" and will be used at one point or the other in the application, so it's not a matter of platform-dependant bundles or splitting my app in DLCs.
     
    Last edited: Aug 24, 2021
  2. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Yes, myPrefab must be loaded before Index is loaded, recursively. These are direct references, and if you think about it there's no way they could be loaded on demand automatically since your code could try to access anything at any moment.

    If you just migrate to asset bundles, you'll still have the same problem: direct references must always be loaded before the objects that reference them can be loaded. If you want to load and unload dynamically, you'd need to either use Resources.Load() or Addresable Assets' asset references.
     
  3. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    303
    Yes, everything is loaded recursively.

    We use asset bundles to start preloading assets asynchronously in main menu
    First asset bundle is loaded through AssetBundle.Load, this is very fast and does not load assets.
    Then AssetBundle.LoadAllAssetsAsync is called and assets start to load asynchronously.
    Player can do whatever he wants in main menu or even start loading the game. When loading the game, we show loading screen, the actual scene load happens AFTER the bundle async load is finished (this is automatic in Unity, loading operations are queued automatically for loading thread).

    Or if you don't want to load everything into memory, you could do this
    Instead of having index, you could have just the bundle and load asset manually when you need it with:
    AssetBundle.LoadAsset
    or
    AssetBundle.LoadAssetAsync

    Or as mentioned above, you could use Addressable Assets. We don't use Addressables, because it's trying to solve problems we don't have and it's not solving what we want effectively.