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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Having trouble with loading groups of assets

Discussion in 'Addressables' started by dthurn, Aug 21, 2018.

  1. dthurn

    dthurn

    Joined:
    Feb 17, 2015
    Posts:
    77
    I'm making a simple card game that uses Addressables, and I want to load all of the card sprites at once. I instantiate a bunch of card prefabs and then I want to assign their sprites properly from images tagged with the label "card". My code for that is something like this:

    Code (CSharp):
    1.       Addressables.LoadAssets<Sprite>("card", null).Completed += (operation) =>
    2.       {
    3.         foreach (var sprite in operation.Result)
    4.         {
    5.           // assign sprite
    6.         }
    7.       };
    The problem is that the list of sprites comes back in a random order, and there's not really an obvious way to make sure the right sprite is assigned to the right logical GameObject. Is there an easy way to set up that relationship, maybe based on looking at the path for each sprite?

    Alternatively, is there an API where I can provide a list of paths to use and be guaranteed to get back the sprites in the provided order?
     
  2. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    Hi,
    This is an interesting use case. A couple of things we could improve to make this easier for you.

    1. As we look up assets, we load all the locations to load into a HashSet. This ensures that we don't accidentally load duplicates. We are using the default GetHashCode, but if our IResourceLocation implemented GetHashCode we could make it deterministic (probably) which could at least provide you consistent (non-random) results. We can't do much better than that for ordering results of a label search. The reason being that there isn't really a "right" order. The addressables window supports sorting, so the assets aren't actually arranged in any order that we could return.
    2. This makes me think we need to extend our AssetReference class... The more robust solution for you, if you need things in a specific order, would be to have your component have an array of AssetReferences. You could then loop them in your code and load them all. The reason this won't work just yet, is that the AssetReference does not keep up with the asset itself after loading. So even if you loaded them all in order, they may not complete in order. Instead you'd want to load them all, then once done, loop them (in order) and get the loaded Asset. We'll add this to the list. (of note, arrays of AssetReferences don't show up well in the UI, but we're working on that too).

    Thanks,
    Bill