Skip to content

Instantly share code, notes, and snippets.

@empika
Created June 25, 2020 15:04
Show Gist options
  • Save empika/35de06712c09f87d9e959772597d53eb to your computer and use it in GitHub Desktop.
Save empika/35de06712c09f87d9e959772597d53eb to your computer and use it in GitHub Desktop.
Addressables LoadAllAssets
public IEnumerator LoadAllAssets<T>(OnLoadAssetsCallback<T[]> callback) where T : Object
{
List<IResourceLocation> locations = getLocationsForType<T>();
AsyncOperationHandle<IList<T>> asyncOperation = Addressables.LoadAssetsAsync<T>(locations, null);
yield return asyncOperation;
if (asyncOperation.Status == AsyncOperationStatus.Succeeded)
{
callback(asyncOperation.Result.ToArray());
}
}
private List<IResourceLocation> getLocationsForType<T>()
{
Dictionary<string, IResourceLocation> locations = new Dictionary<string, IResourceLocation>();
foreach (IResourceLocator locator in Addressables.ResourceLocators)
{
foreach (object key in locator.Keys)
{
if (!locator.Locate(key, typeof(T), out var keyLocation))
continue;
IResourceLocation location = keyLocation[0];
if (!locations.ContainsKey(location.PrimaryKey))
{
locations.Add(location.PrimaryKey, location);
}
}
}
return locations.Select(l => l.Value).ToList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment