Search Unity

Why InitializationLoader._menuLoadChannel is referenced as AssetReference and not directly as SO?

Discussion in 'Open Projects' started by esteban16108, Apr 26, 2022.

  1. esteban16108

    esteban16108

    Joined:
    Jan 23, 2014
    Posts:
    158
    Hi,

    As many before me, I came to this project to learn good architecture practices, and I'm just learning about Unity Addressables.

    I was looking thru the Initializer and found the the Event LoadMenu_Channel is being referenced as AssetReference and not directly as SO (LoadEventChannelSO), and has to be loaded async to be used.

    Is this because the use of Addressables as a whole, or?

    Code (CSharp):
    1.     [Header("Broadcasting on")]
    2.     [SerializeField] private AssetReference _menuLoadChannel = default;
     
  2. esteban16108

    esteban16108

    Joined:
    Jan 23, 2014
    Posts:
    158
    OK I tried to directly refence as

    Code (CSharp):
    1.     [Header("Broadcasting on")]
    2.     [SerializeField] private LoadEventChannelSO _menuLoadChannel = default;
    And changed the code needed to load the Menu skipping the Async loading part of the previous AssetReference and it works.

    Is there any specific nuance on why was being done like that?

    Thanks
     
  3. Harsh-NJ

    Harsh-NJ

    Joined:
    May 1, 2020
    Posts:
    315
    Nope, that is not how it is supposed to work. I feel like you don't know how addressables do work!

    What Addressables do is it packages all the Assets into so called Asset Bundles. (but in a smarter way, you can get more info on the Addressables package docs)

    Then we use an AssetReference to reference that asset we want to load from the AssetBundle. Loading is done asynchronously.
    What this benefits in is let's say you reference 50 SO in a scene, then all those 50 had to be loaded into memory. But if we use AssetReference, none of them are loaded, and any is only loaded when and if needed.

    So what we do is store references to assets, they don't load into the memory, and if anyone is needed, then we load it, if not already loaded, then use it. This maybe overkill for small projects, but a huge memory saver for big projects (like ChopChop)

    Now you got it, right??
    I did my best to make you understand. But the more better place to read about this is, obviously Addressables docs...
    Feel free to ask!