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 Understanding Addressables: When do they need to be included as AssetReference?

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

  1. Mauschelbaer

    Mauschelbaer

    Joined:
    Aug 4, 2019
    Posts:
    17
    I am having trouble understanding exactly how to use AdressableAssets.

    In my (non-addressable scene) "Init", I am trying to reference a ScriptableObject and a scene (named "mainMenu").
    I do this via:

    Code (CSharp):
    1.  
    2. public class Initializer : MonoBehaviour
    3. {
    4.     [SerializeField] private AssetReference mySUPERAssetAF;
    5.     [SerializeField] private AssetReference mainMenuSceneAF;
    6.  
    7. //...
    8.  
    9.     void Start()
    10.     {
    11.             // load the next scene
    12.             mainMenuSceneAF.LoadSceneAsync(LoadSceneMode.Additive).Completed += (obj) => {
    13.                 // load the asset
    14.                   mySUPERAssetAF.LoadAssetAsync().Completed += (obj) => {
    15.                     obj.Result.doSomething();
    16.                 };
    17.             };
    18.     }
    19. }
    20.  
    In the addressable-scene ("mainMenu") loaded above, I also have a script that should use the same SuperAsset.
    I noticed that I do NOT have to include it there via AssetReference, but "directly":

    Code (CSharp):
    1.  
    2. public class MainMenuController : MonoBehaviour
    3. {
    4.     [SerializeField] private SuperType mySUPERAsset;
    5.  
    6. //...
    7.  
    8.     void Start()
    9.     {
    10.         mySUPERAsset.doSomething();
    11.     }
    12. }
    13.  
    By the way: Why the two scenes should use the same SuperAsset is because in practice they are ScriptableObjects, whereby both should work on the same instance. In this way, I create an event system, so to speak. But that's just by the way...

    Can someone explain to me why this works and why I don't have to access the asset via AssetReference in the MainMenu-Scene?

    Thank you in advance
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    Because if your Main Menu scene is addressable, then all dependencies of the scene are loaded at the same time.

    Where as non-addressable scenes need to load in addressable assets via asset references, otherwise you get assets duplicated at build time.
     
    Mauschelbaer likes this.
  3. Mauschelbaer

    Mauschelbaer

    Joined:
    Aug 4, 2019
    Posts:
    17
    Hey @spiney199 , thank you for your answer! That helped me a lot!