LoadAssetsAsync fails but LoadResourceLocationsAsync succeeds

We have shrunk our app bundle from 350 mb to less than 100 using addressables. Nice!

Now I need to implement a loading bar. But to do that I need to load the assets as a list. So far all of the assets are loaded individually.

I am trying to load a list of assets using LoadAssetsAsync. My keys look like
“Assets/UI/Prefabs/Misc/HUD/CurrencyHUD.prefab” for example.

Here is a method with 3 attempts to load. The first 2 fails. The 3rd succeeds, but loads individual assets in a loop. They all use the same keys.

    public async Task Load(Action<AsyncOperationHandle> completed)
    {
        // This fails
        AsyncOperationHandle handle = Addressables.LoadAssetsAsync<UnityEngine.Object>(keyMap.Keys, (gameObject) =>
        {
            Debug.Log($"Load: got object {gameObject.name}");
        });

        // Result.Count is always 0
        var validateAddress = Addressables.LoadResourceLocationsAsync(keyMap.Keys);
        await validateAddress.Task;
        if (validateAddress.Status == AsyncOperationStatus.Succeeded)
        {
            if (validateAddress.Result.Count > 0)
            {
                Debug.Log($"Load: got resource location");
            }
        }

        // This succeeds
        int numLoaded = 0;
        List<Task> tasks = new List<Task>();
        foreach (var keyValuePair in keyMap)
        {
            string key = keyValuePair.Key;
            AsyncOperationHandle<IList<IResourceLocation>> h = Addressables.LoadResourceLocationsAsync(key);
            Assert.IsFalse(handles.ContainsKey(key));
            handles[key] = h;
            h.Completed += (handle) =>
            {
                Log($"Load: Completion for key {key}. Resource locations:");
                List<IResourceLocation> locations = new List<IResourceLocation>(handle.Result);
                foreach (var resourceLocation in handle.Result)
                {
                    Log($"Load result: {resourceLocation.PrimaryKey} {resourceLocation.ResourceType} {resourceLocation.InternalId}");
                    ++numLoaded;
                }
            };

            tasks.Add(h.Task);
        }

        await Task.WhenAll(tasks.ToArray());
        tasks.Clear();
        keyMap.Clear();

        completed.Invoke(handle);
    }

So why do the list operations fail to load any of the assets? The results from the individual asset calls all show keys that match the keys in my list operations.

I would love to see example that uses addresses as keys. The examples I have found all seem to use labels, like “enemy” rather than asset addresses.

Does anybody have an example of using Addressables.LoadAssetsAsync or Addressables.LoadResourceLocationsAsync with a list of keys?

Or am I way off base here?

In order to use the API with a list of keys, you must tell it how you want those keys to be used. Loading Addressable assets | Addressables | 1.19.19 explains the merge modes and when to use them. In your example, you want Union.

Thanks Andy, I added the merge mode and it works now.

I see what happened. The overload of Addressables.LoadAssetsAsync that takes a callback parameter assumes MergeMode.Intersection. So the solution was to use the non-callback version add the merge mode.