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

Finding the bundle name from an addressable address

Discussion in 'Addressables' started by LarsGameloft, Aug 27, 2019.

  1. LarsGameloft

    LarsGameloft

    Joined:
    Jun 4, 2019
    Posts:
    28
    Hi,
    I have this download class that shows the download size of all addresables being downloaded. If many assets get downloaded I sum them up. Problem is that if I call GetDownloadSizeAsync on many assets that are in the same bundle I will get the size of the bundle everytime, I would like to only add sizes of assets being downloaded from different bundles, I can do it by comparing the bundle names and ignoring duplicates, but I cannot find a way to get the asset bundle name from an asset key/address.
     
    Last edited: Aug 30, 2019
  2. nik_d

    nik_d

    Joined:
    Apr 27, 2018
    Posts:
    66
    You can dig down into resource locations and collect their dependencies, something like this:

    Code (CSharp):
    1.         private readonly HashSet<IResourceLocation> _allDeps = new HashSet<IResourceLocation>();
    2.         private readonly List<(IResourceLocation dep, long size)> _requiredDeps = new List<(IResourceLocation, long)>();
    3.  
    4.         private static bool GetResourceLocations(object key, out IList<IResourceLocation> locations) =>
    5.             Addressables.m_Addressables.GetResourceLocations(key, out locations);
    6.         public void Add(IEnumerable<IResourceLocation> locations)
    7.         {
    8.             foreach (var location in locations) {
    9.                 if (location == null || !location.HasDependencies) continue;
    10.                 foreach (var dep in location.Dependencies) {
    11.                     if (!_allDeps.Add(dep)) continue;
    12.                     if (_isRequred(dep, out var size)) { //new dependency
    13.                         TotalSize += size;
    14.                         _requiredDeps.Add((dep, size));
    15.                     }
    16.                 }
    17.             }
    18.         }
    19.  
    20.         private static bool _isRequred(IResourceLocation dep, out long size)
    21.         {
    22.             if (dep.Data is ILocationSizeData locationSizeData) {
    23.                 size = locationSizeData.ComputeSize(dep);
    24.                 if (size > 0) return true;
    25.             } else size = 0;
    26.             return false;
    27.         }
    28.  
     
  3. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    In our next minor-version-bump release (1.2.something) out soon, we're adding a
    GetDownloadSizeAsync(IList<object> keys)
    interface that'll handle shared bundle dependencies properly.
     
  4. nik_d

    nik_d

    Joined:
    Apr 27, 2018
    Posts:
    66
    GetSize(list) is not enough to show real download progress (in bytes) of *partially* downloaded bundles.

    Look:
    Let asset A1 depends on bundles [b1], asset A2 on [b1,b2] and let assume we already downloaded A1.
    So GetSize(A1,A2) gives us only the [b2] size, but Load(A1,A2).progress gives us 0.5 before download even started!

    And so the progress message like "{GetSize(A1,A2) * Load(A1,A2).progress} bytes downloaded" shows completely wrong numbers.

    That's why I only collect dependencies *that not in cache* and try to pre-load only them.
     
  5. nchandrahas

    nchandrahas

    Joined:
    Feb 4, 2019
    Posts:
    7
    How can we get the exact bundle name using the address of an asset in editor mode? The catalog doesn't contain any specific mapping instead I see only InternalIds.