Search Unity

Question When are the resources used by a prefab loaded?

Discussion in 'Editor & General Support' started by kodra_dev, Nov 19, 2022.

  1. kodra_dev

    kodra_dev

    Joined:
    Oct 31, 2022
    Posts:
    108
    For example, I have a scene. The scene has a GameObject. The GameObject has reference to a prefab, like this:

    Code (CSharp):
    1. class MyGameObject : MonoBehaviour
    2. {
    3.     [SerializedField] private GameObject myPrefab;
    4. }
    And I assign a prefab to it in the inspector.

    The prefab has references to some really huge resources, e.g. a MeshFilter with a hi-res mesh.

    Now my question is: when is the mesh loaded? When I load the scene? Or when I instantiate the prefab for the first time? Also, is there a "resource loading debugger" where I can find out the answers myself?

    And how could I finetune this behavior? For example, if my gameObject refers to 20 prefabs, can I control which resources to be loaded, and show a loading screen while doing that?
     
    chriseborn likes this.
  2. kodra_dev

    kodra_dev

    Joined:
    Oct 31, 2022
    Posts:
    108
    Bump. I mean this is quite an essential question, right?
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,923
    Direct serialised references like the one you have above - ergo dependencies - are loaded into memory when their appropriate scene is loaded. Thus if this perfab has some heavy assets, those are loaded in too.

    If you want more control over when something is loaded into memory, you can use the Addressables package.
     
    kodra_dev likes this.
  4. kodra_dev

    kodra_dev

    Joined:
    Oct 31, 2022
    Posts:
    108
    Thank you. I was thinking of Resources, but it seems be obsolete and replaced by Addressables, right?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    So I'm told.

    I still use
    Resources.Load<T>()
    because it has a few important advantages over Addressables:

    1. it's built-in and requires almost zero set up (just put your assets under a
    Resources/
    folder)
    2. it does what I need
    3. I don't need to install and learn a massive hairy package with 57,000 different settings to load a file at runtime.

    I won't touch Addressables unless I actually NEED their extra functionality, which is awesome and powerful, but FAR beyond what I need for my games.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,923
    Resources is simple and good for small amounts of runtime loading.

    Anything large scale, you'll want to use Addressables, which is build on top of asset bundles.

    It's honestly not that complicated. It works out of the box; you only have to fiddle with settings if you're dealing with content delivery, at which point, there's always going to be settings.
     
    Kurt-Dekker likes this.