Created
June 25, 2020 15:04
-
-
Save empika/35de06712c09f87d9e959772597d53eb to your computer and use it in GitHub Desktop.
Addressables LoadAllAssets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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