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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question How to get correct download size for an IResourceLocation list?

Discussion in 'Addressables' started by yusuf_isik, Feb 21, 2023.

  1. yusuf_isik

    yusuf_isik

    Joined:
    Feb 14, 2014
    Posts:
    21
    Hello. I'm creating a pre-caching system for my game with addressables. My workflow is like that:

    1- Use Addressables.InitializeAsync() to get the IResourceLocator.
    2- Use IResourceLocator to Locate all IResourceLocations (IResourceLocator.Locate())
    3- Use Addressables.GetDownloadSizeAsync() to get the download size for these locations.
    4- Sum all sizes and find the total download size.
    5- Use Addressables.DownloadDependenciesAsync() for the location list to cache the required assets.

    In this workflow, there is a problem. In the 3rd step, Addressables.GetDownloadSizeAsync() returns the required download size for a location, but all locations in the same asset group return the same size. Therefore if there are 10 assets in an asset group, and if I loop all of them to get the total size from the list of the IResourceLocation, it will be 10 times bigger in total.

    Is there a way to get only asset group size in this workflow? Or how should I approach the pre-caching scenario?

    Note: I'm using Unity 2020.3.28.f1 - Addressables 1.18.19
     
  2. MaybeAnAlien

    MaybeAnAlien

    Joined:
    Feb 20, 2023
    Posts:
    1
    Try a single call to Addressables.GetDownloadSizeAsync with the list of all the locations at once.
     
    yusuf_isik likes this.
  3. andymilsom

    andymilsom

    Unity Technologies

    Joined:
    Mar 2, 2016
    Posts:
    294
    That will work to do GetDownloadSize with all locations.

    You could also get it directly and have more control and be able to identify every Bundle and do some logic if needed. Like so:

    Code (CSharp):
    1. long size = 0;
    2. HashSet<IResourceLocation> done = new HashSet<IResourceLocation>();
    3. foreach (IResourceLocator locator in Addressables.ResourceLocators)
    4. {
    5.     ResourceLocationMap mapping = locator as ResourceLocationMap;
    6.     if (mapping != null)
    7.     {
    8.         foreach (KeyValuePair<object,IList<IResourceLocation>> mappingLocation in mapping.Locations)
    9.         {
    10.             foreach (IResourceLocation resourceLocation in mappingLocation.Value)
    11.             {
    12.                 if (resourceLocation.Data != null && !done.Contains(resourceLocation))
    13.                 {
    14.                     done.Add(resourceLocation);
    15.                     var sizeData = resourceLocation.Data as ILocationSizeData;
    16.                     if (sizeData != null)
    17.                         size += sizeData.ComputeSize(resourceLocation, Addressables.ResourceManager);
    18.                 }
    19.             }
    20.         }
    21.     }
    22. }
     
    SonSep and yusuf_isik like this.