Search Unity

Question Download specific addressable gameobjects from catalog

Discussion in 'Addressables' started by C0lonnello, May 24, 2023.

  1. C0lonnello

    C0lonnello

    Joined:
    Nov 7, 2022
    Posts:
    30
    Hi,
    I'd like to know if there is a way to fetch all the files that are bundled in a given addressable group at runtime without hardcoding or manually inserting a list of keys.

    More specifically, I'd like to release a player build which fetch a list of addressable prefabs I created and shows the user this list in order to let him choose which environment load in the current scene. I'd like to be able to add a new one prefab environment to the addressable remote path without changing and uploading the player build and without manually uploading any kind of text list or json file.

    I'd like also to not load all the whole addressable bundle at start, it would be too big, I want to have this list in order to choose one specific "id" of the environment the user wants to load and so load that specific prefab only.
     
    RRD123 likes this.
  2. RRD123

    RRD123

    Joined:
    Aug 24, 2022
    Posts:
    8
    I am also looking for the same. I feel the previous Asset Bundle workflow was better in this criteria.
     
  3. Phan-Phantz

    Phan-Phantz

    Joined:
    Nov 11, 2015
    Posts:
    17
    Looks like there are no simple solution for this right now. According to this thread.

    There are many workarounds, for instance,
    • Store a list of desired assets on backend and use that list to load multiple assets at once.

    • Label multiple assets using the same name and load by label.

    • Making a folder 'Addressables' and label this folder. You will be able to load assets by label. To load sub assets inside an Addressables folder, use FolderAddress/SubAssetName.FileExtension
     
  4. RRD123

    RRD123

    Joined:
    Aug 24, 2022
    Posts:
    8
    I have a library of gameobjects which all bundled and uploaded to a server. Each gameobject is unique. When I press a button, I wanted to load only a specific gameobject from that bundle. Moreover, I won't even know which gameobject I will be adding into that bundle in the future. So I won't be having a static label reference in my app. I want to dynamically pull that one particular gameobject from that bundle. I don't think this is possible with 'Addressables'. Correct me If I am wrong.
     
    C0lonnello likes this.
  5. C0lonnello

    C0lonnello

    Joined:
    Nov 7, 2022
    Posts:
    30
    That is the same goal that I tried to reach.

    The workaround I used at this point is to divide every "big" unique gameobject in groups where every group contains one and only prefab object. In this way if i ask to download a specific key for a gameobject, it will download only the bundle that contains that gameobject. The addressable build folder will look like a list of "big" files named as [Group_name]_assets_all_xxxxxxxxxxxxxx.bundle, a "General" asset group that contains things which are used by all gameobjects and the catalog hash/json.

    With this method it is working as intended, I don't know if it is the better way to manage it, I didn't have so much time to experiment (and the internet wasn't really helpful).

    On the other hand, that means that every uploaded gameobject needs to be manually divided in a group, naming the group, setting the group as Remote; maybe with some custom editor functions it is possible to speed up this logic a bit.

    I'm quite surprised how so few people talked about this matter while it is such a frequent situation since metaverse / business Unity applications.
     
    RRD123 likes this.
  6. RRD123

    RRD123

    Joined:
    Aug 24, 2022
    Posts:
    8
    Yes. The functionality that we need can be achieved using the conventional asset bundle. It is quite surprising that this functionality has not been included in the addressables. Or, who knows I am missing something. Terrible. Anyway, I felt this documentation slightly boosted my thought process on achieving my goals. From my understanding, it loads all the locations. From the list of locations, by choosing one specific location of a gameobject that we need to download, we can load that one specific gameobject. Here is the code snippet.

    Code (CSharp):
    1.  public async void LoadSelectedAsset(string assetPath)
    2.     {
    3.         await GetAssetWithLabel(assetLabelReference, assetPath);
    4.     }
    5.  
    6.     public async Task GetAssetWithLabel(AssetLabelReference label, string path)
    7.     {
    8.         var locations = await Addressables.LoadResourceLocationsAsync(label).Task;
    9.  
    10.         foreach(var location in locations)
    11.         {      
    12.             if(location.ToString() == path)
    13.             {
    14.                 if (tempObj != null) Destroy(tempObj);
    15.                 obj = await Addressables.LoadAssetAsync<GameObject>(location).Task;              
    16.                 tempObj = Instantiate(obj);
    17.                 Debug.Log("Location: " + location);
    18.             }          
    19.         }
    20.     }
    However, to my surprise, the moment I load one asset, all the other assets from that label is also downloading. Can you find something with this?
     
  7. C0lonnello

    C0lonnello

    Joined:
    Nov 7, 2022
    Posts:
    30
    Yes I can confirm that I, too, am using the location list to fetch my addressables, but as I mentioned previously, in order to have my addressables divided in different files (bundles) and so to be able to download them singolarly I need to divide them into groups, where each of them in the build process will be included in a dedicated file.

    If you don't do that, the addressable build file will be just a single big bundle, and that will be downloaded as a whole when something inside it is requested, as far as I understood.

    I did something like this:
    Screenshot (37).png
     
    RRD123 likes this.
  8. RRD123

    RRD123

    Joined:
    Aug 24, 2022
    Posts:
    8
    I tried the same workflow as yours and it is working as expected. I can now download each asset individually on a button click. Below is my code, for anyone who look for the same.

    Code (CSharp):
    1. public void LoadSpecificAsset(string path)  //assign this to a button
    2.     {
    3.         StartCoroutine(DownloadAssetFromGroup(path));
    4.     }
    5.  
    6.     IEnumerator DownloadAssetFromGroup(string path)
    7.     {
    8.         AsyncOperationHandle<IList<IResourceLocation>> locations = Addressables.LoadResourceLocationsAsync(assetLabelReference, null);
    9.         yield return locations;
    10.  
    11.         foreach(IResourceLocation location in locations.Result)
    12.         {
    13.             if(location.ToString() == path)
    14.             {
    15.                 Addressables.LoadAssetAsync<GameObject>(path).Completed += (opHandle) =>
    16.                 {
    17.                     if(opHandle.Status == AsyncOperationStatus.Succeeded)
    18.                     {
    19.                         if (tempObj != null) Destroy(tempObj);
    20.                         tempObj = Instantiate(opHandle.Result);
    21.                         Debug.Log("Asset: " + path);
    22.                     } else
    23.                     {
    24.                         Debug.Log("Failed to load asset !");
    25.                     }
    26.                 };
    27.             }
    28.         }
    29.  
    30.         Addressables.Release(locations);
    31.     }
     
    C0lonnello likes this.
  9. C0lonnello

    C0lonnello

    Joined:
    Nov 7, 2022
    Posts:
    30
    I have an update on this matter, I feel I should have found it sooner but as mentioned i didnt find anything on the web for this case.

    In order to download objects separately, you can add all of the in a group, and then in the group profile you can set the "Bundle Mode" setting as "Pack Separately" or "Pack by Label", which are self-explanatory. In this way we can have automatically divided bundles that I believe will be downloaded separately.
     
    RRD123 likes this.
  10. RRD123

    RRD123

    Joined:
    Aug 24, 2022
    Posts:
    8
    Ohh.. Now I feel dumb :p Anyway, very useful find. Thank you so much for helping me out in this.