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

Load all references by label

Discussion in 'Addressables' started by calpolican, Jan 7, 2020.

  1. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    I'm trying to load all the asset references that share a certain label. I don't want to load all the assets into memory, just the references. The idea is to be able to load a random sound within a list of possible clips.
    I haven't been able to do it, even though in a Unity confrence by Arturo Nunez, this was done.
    This is one of the many codes I tried:
    Code (CSharp):
    1. Addressables.LoadAssetsAsync<IResourceLocation>(myLabel.labelString, null).Completed += objects =>
    2.         { myObjRefList = new List<IResourceLocation>(objects.Result);};
    3.  
     
    Last edited: Jan 7, 2020
    Tony_Max likes this.
  2. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    Same for me -- I'm trying to access the assets guids to load the same asset on both client and server
     
  3. Shaunyowns

    Shaunyowns

    Joined:
    Nov 4, 2019
    Posts:
    328
    Will send this over @calpolican to see if we can get an answer that helps you!
     
    Tony_Max and calpolican like this.
  4. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    +1!

    I'm interested too, no one seems to know how the simplest use case for this works, a simple load into memory of all addressables sharing the same label.
     
  5. WaaghMan

    WaaghMan

    Joined:
    Jan 27, 2014
    Posts:
    245
    This code works for me:
    Code (CSharp):
    1.  
    2. const string PreloadLabel = "preload";
    3.  
    4. static Dictionary<string, UnityEngine.Object> resources = new Dictionary<string, UnityEngine.Object>();
    5.  
    6.        
    7. static bool preloaded = false;
    8.         public static bool Preloaded { get { return preloaded; } }
    9.  
    10.        
    11. public static async Task Preload()
    12.         {
    13.             Debug.Log("Preloading Addressables. Time: " + Time.realtimeSinceStartup + " Frame: " + Time.frameCount);
    14.  
    15.             var list = await Addressables.LoadResourceLocationsAsync(PreloadLabel, null).Task;
    16.  
    17.             if (list == null) return;
    18.  
    19.             List<string> keys = new List<string>(list.Count);
    20.  
    21.             foreach (var key in list)
    22.             {
    23.                 keys.Add(key.PrimaryKey);
    24.             }
    25.  
    26.             var r2 = await Addressables.LoadAssetsAsync<UnityEngine.Object>(list, null).Task;
    27. if (r2 != null)
    28.             {
    29.                 if (r2.Count != keys.Count)
    30.                 {
    31.                     Debug.LogError("Loaded objects count differs from key count!");
    32.                     return;
    33.                 }
    34.  
    35.                 for (int i = 0; i < keys.Count; i++)
    36.                     resources.Add(keys[i], r2[i]);
    37.  
    38.                 Debug.Log("Addressables Preloaded. Time: " + Time.realtimeSinceStartup + " Frame: " + Time.frameCount);
    39.  
    40.             }
    41.  
    42.             preloaded = true;
    43.  
    44.         }
     
    Lekret and calpolican like this.
  6. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    Oh, that's great, thanks Waaghman.

    So, to get all the references:
    Code (CSharp):
    1.  
    2. public AssetLabelReference miLabel; //Select the label you want to load in the inspector
    3. IList <IResourceLocation> myRefList; //List to store all the references.
    4.  
    5. //Function to load all the references.
    6. Addressables.LoadResourceLocationsAsync(miLabel.labelString, null).Completed += x => { myRefList = x.Result;};
    7.  
    You can use the reference.Primary key to make a dictionary, but I'm not sure why would you'd need it, I mean, if you have the key, you can just load the asset with it.
     
    Last edited: Jan 17, 2020
    Oshigawa likes this.
  7. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    You can sort them to have the same order on both server and client and reference them by index
     
  8. WaaghMan

    WaaghMan

    Joined:
    Jan 27, 2014
    Posts:
    245
    That code was done for a preload system, in order for addressables to work like Resources.Load(). The game would preload all assets with the "preload" label upon load, and then, anywhere from the code, we can just instantiate assets synchronously by calling a method with a string (such as "Prefabs/AmmoDrop").

    So we wouldn't have a key object, just the string. Thus the dictionary.
     
  9. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    425
    Ok, thanks for clarifying that.
     
  10. prinoldmj

    prinoldmj

    Joined:
    Feb 10, 2017
    Posts:
    8
    How would we get a List of AssetReference that has a specific label as opposed to IResourceLocation?

    Edit:
    Nevermind. i just needed to alter my LoadAssetAsync call to use the IResourceLocation as opposed to calling from the AssetReference
     
    Last edited: Dec 17, 2020