Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question When to use references directly, when via AssetReference?

Discussion in 'Addressables' started by Mauschelbaer, Sep 3, 2023.

  1. Mauschelbaer

    Mauschelbaer

    Joined:
    Aug 4, 2019
    Posts:
    17
    I am currently trying to better understand Unity techniques by means of an example project.
    The project is designed so that many scenes are bundled into AssetBundles.
    At one point, when it comes to addressable assets and scriptable objects, I don't understand why different approaches are chosen in the example.

    The author links the AddressableObject sometimes as AssetReference, which has to be loaded async, but sometimes also as a diect reference.

    My question is therefore: What difference could there be between the following situations:

    Code (CSharp):
    1. [SerializeField] private AssetReference mySOAssetReference;
    2.  
    3. private void DoSomething()
    4. {
    5.     mySOAssetReference.LoadAssetAsync().Completed += (mySO) => {
    6.         myAsset.Result.DoItNow();
    7.     }
    8. }
    V.S.

    Code (CSharp):
    1. [SerializeField] private MyScripableObject mySO;
    2.  
    3. private void DoSomething()
    4. {
    5.     mySO.DoItNow();
    6. }
    By the way: It's about the former Unity Open Project "Chop Chop". The author uses this procedure to exchange events between several scenes. In the initialisation scene the event is loaded as AssetReference, but in the scene "PersistenceManagers" the same ScriptableObject is accessed via direct reference.
     
  2. gooby429

    gooby429

    Joined:
    Aug 13, 2019
    Posts:
    116
    I would say the main difference is that addressables allows you to free up memory when youre done with an SO by releasing it. A direct reference will always be loaded into memory. This usually isn't a big issue unless you have a ton of these or very heavy SOs

    I'm not familiar with that project but I'd assume they would load it by asset ref in startup so it can do it's thing then release it from memory when it's done. Where as the persistent one might always be loaded anyway, so may as well use a direct ref. (Could also be an asset ref if u really wanted to)
     
    Mauschelbaer likes this.
  3. Mauschelbaer

    Mauschelbaer

    Joined:
    Aug 4, 2019
    Posts:
    17
    Ah, I see, thank you for the answer!

    Would I have to unload the AssetRefrence manually, or would that happen automatically when the "init" scene (the one that contains the script that loads the SO) is unloaded?
     
  4. gooby429

    gooby429

    Joined:
    Aug 13, 2019
    Posts:
    116
    you have to unload it yourself, so going off your example youd just release the handle after DoItNow()
     
    Mauschelbaer likes this.