Search Unity

Get list of dependencies via code

Discussion in 'Addressables' started by MagicDesignEmerick, Oct 16, 2019.

Thread Status:
Not open for further replies.
  1. MagicDesignEmerick

    MagicDesignEmerick

    Joined:
    Oct 4, 2017
    Posts:
    26
    I'm looking into addressables for our loading process, and I'd like to know if there was a way in the API to get the dependencies of an addressable asset.

    While there is a way to download in advance the dependencies of an addressable, I can't find a way to infer from this the list of assets effectively downloaded. (edit : see below)

    I'd like to get this list from an editor script, as it would be useful for our build process.

    Edit : I manageed to get the dependencies working in editor (using
    Addressables.LoadResourceLocationsAsync(asset); to which I supplied the wrong key before).
    But it's not working in the editor, from what I gathered the Addressable system cannot init in edit mode)
     
    Last edited: Oct 16, 2019
    JesseSTG and csteinberg like this.
  2. Jribs

    Jribs

    Joined:
    Jun 10, 2014
    Posts:
    154
    @MagicDesignEmerick Did you ever find a solution for this?

    Addressables.LoadResourceLocationsAsync(asset) doesn't give a list of dependencies. It just returns the items that are referenced by the given key.

    Edit: To clarify what I am looking for. I want to be able to know which bundles, not specific assets, will be downloaded when I do
    DownloadDependenciesAsync("myAsset")

    And I need to write an editor script that will spit out that information
     
    Last edited: Dec 6, 2019
  3. BrokenAngel

    BrokenAngel

    Joined:
    Mar 24, 2013
    Posts:
    92
  4. Jribs

    Jribs

    Joined:
    Jun 10, 2014
    Posts:
    154
    @BrokenAngel it sounds like you are having a different issue, and you just need to use
    DownloadDependenciesAsync("myAsset")
    which will download everything required to load whatever key you put in.
     
  5. BrokenAngel

    BrokenAngel

    Joined:
    Mar 24, 2013
    Posts:
    92
    But I need to know which key is, and where I can get those keys ? since I want the application dynamically downloading all the "keys" I need in the server.
     
  6. MagicDesignEmerick

    MagicDesignEmerick

    Joined:
    Oct 4, 2017
    Posts:
    26
    @Jribs Unfortunately no, I've resorted to manually keep track of dependancies in our scenes with some calbacks when saving scenes.
     
  7. JonathanBartel

    JonathanBartel

    Joined:
    Jun 24, 2019
    Posts:
    47
    @MagicDesignEmerick @BrokenAngel @Jribs

    If I'm understanding you correctly, you want to know the names of the bundles that contain the dependencies for a given key.

    Addressables.LoadResourceLocationsAsync(key)
    is half-way to what you're looking for. Try this:

    Code (CSharp):
    1. public IEnumerator GetDependencies(string key)
    2. {
    3.    var locs = Addressables.LoadResourceLocationsAsync(key);
    4.  
    5.     while (!locs.IsDone) yield return null;
    6.  
    7.     foreach (var loc in locs.Result)
    8.         foreach (var dep in loc.Dependencies)
    9.             Debug.Log($"dep.PrimaryKey = {dep.PrimaryKey}");
    10. }
    The
    locs.Result
    is a list, but should only contain 1 variable.
    loc.Dependencies
    returns a list of the bundle names containing your key's dependencies. The bundle names include folder paths if your bundle structure uses folders (ex: folder/folder/dependency.bundle) This list also includes the bundle for your key.

    I'm using Addressables 1.5.0. I'm not entirely sure when this language became available.

    EDIT: Don't forget to load your content catalog. I didn't include that step in my example.
     
    Last edited: Jan 2, 2020
    doneykoo likes this.
  8. MagicDesignEmerick

    MagicDesignEmerick

    Joined:
    Oct 4, 2017
    Posts:
    26
    Thanks for the detailed answer, the documentation isn't really clear on what to do with the locations .

    Unfortunately I still can't use that for our use case, but I guess the system isn't meant to be usable from the editor side for now.
     
  9. JonathanBartel

    JonathanBartel

    Joined:
    Jun 24, 2019
    Posts:
    47
    And I take it you have haven't tried AssetDatabase.GetDependencies? In my editor script, I use it in conjunction with AssetDatabase.GetAssetPath to look something like this:

    string[] deps = AssetDatabase.GetDependencies(AssetDatabase.GetAssetPath(object));


    That returns an array with all 500-some assets that my
    object
    is dependent on.
     
  10. MagicDesignEmerick

    MagicDesignEmerick

    Joined:
    Oct 4, 2017
    Posts:
    26
    Indeed I haven't. I was so focused on getting the infos froms adressabels/bundles that I didn't explore the AssetDatabase side.
    I ended up catching what we were looking for as far as dependancies for now via reflection, but I'll explore that possibility for the next refactor, thanks for pointing it out.
     
    JonathanBartel likes this.
  11. Jribs

    Jribs

    Joined:
    Jun 10, 2014
    Posts:
    154
    This is the code I came up with to accomplish my goal.
    My goal is to be able to give an addressable key, which could be an address or label, and the end result would be a list of the bundles that would be needed to be able to load that addressable. This gives the literal bundle name of the built bundles.

    I didn't really go through this to pull out my specific needs. But i don't think theres much specific to me in this.

    Code (CSharp):
    1. private static List<string> GetDependenciesForKey(string addressablesKey)
    2.     {
    3.         var dependenciesList = new List<string>();
    4.         // If this is a label, we want to go and grab all the assets with that label, either way we want to start going through all the entries
    5.         AddressableAssetSettings aaSettings = AddressableAssetSettingsDefaultObject.Settings;
    6.  
    7.         var assetsToCheckList = new List<string>();
    8.         foreach (AddressableAssetGroup addressableGroup in aaSettings.groups)
    9.         {
    10.             foreach (AddressableAssetEntry groupEntry in addressableGroup.entries)
    11.             {
    12.                 // We check to see if the key is a label for any assets or if the key is an address of anything
    13.                 // If it is, we add it to our assets to check list so we can use those to build our dependencies
    14.                 if (groupEntry.address == addressablesKey || groupEntry.labels.Contains(addressablesKey))
    15.                 {
    16.                     if (!assetsToCheckList.Contains(groupEntry.AssetPath))
    17.                         assetsToCheckList.Add(groupEntry.AssetPath);
    18.  
    19.                     // Lets create the bundle file name for whatever asset this is a part of now
    20.                     string bundleFileName = GetBundleFileName(groupEntry);
    21.                     if (!dependenciesList.Contains(bundleFileName))
    22.                         dependenciesList.Add(bundleFileName);
    23.                 }
    24.             }
    25.         }
    26.  
    27.         foreach (string assetPath in assetsToCheckList)
    28.         {
    29.             List<string> childDependencies = GetAddressableDependenciesByPath(assetPath);
    30.             dependenciesList = dependenciesList.Union(childDependencies).ToList();
    31.         }
    32.  
    33.         return dependenciesList;
    34.     }
    35.  
    36.     private static List<string> GetAddressableDependenciesByPath(string filePath)
    37.     {
    38.         AddressableAssetSettings aaSettings = AddressableAssetSettingsDefaultObject.Settings;
    39.         string[] deps = AssetDatabase.GetDependencies(filePath);
    40.         var dependenciesList = new List<string>();
    41.  
    42.         foreach (string dep in deps)
    43.         {
    44.             string guid = AssetDatabase.AssetPathToGUID(dep);
    45.  
    46.             AddressableAssetEntry entry = aaSettings.FindAssetEntry(guid);
    47.             if (entry == null)
    48.                 continue;
    49.  
    50.             string bundleFileName = GetBundleFileName(entry);
    51.             if (!dependenciesList.Contains(bundleFileName))
    52.                 dependenciesList.Add(bundleFileName);
    53.         }
    54.  
    55.         return dependenciesList;
    56.     }
     
  12. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
    Hate to necro a sorta old post, but seems worth adding here instead of making a new post.

    Would be nice if the dependencies of a group could be displayed in the editor out of the box so that non-developers (aka level designers) could see how many bundles the map they are working on will have to download while working on it.

    Going to create an internal tool to do this for my company, just figured id suggest it here as well.
     
  13. LilMako17

    LilMako17

    Joined:
    Apr 10, 2019
    Posts:
    43
    Dahaka444 and sniffle63 like this.
  14. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,820
    Closing this thread to avoid future necroposting. If you'd like to continue discussing this, feel free to create a new thread. :)
     
Thread Status:
Not open for further replies.