I've managed to come up with a basic solution for the following problem. Here's an example of needing to load and store sprites:
- Add your
addressables
into a group and label them accordingly. In my case,"pictures"
.
Add the code.
public static readonly Dictionary<string, Sprite> AllSprites = new Dictionary<string, Sprite>(); public static async Task InitSprites(string assetLabel) { var locations = await Addressables.LoadResourceLocationsAsync(assetLabel, typeof(Sprite)).Task; List<Task<Sprite>> tasks = new List<Task<Sprite>>(); foreach (var location in locations) { tasks.Add(Addressables.LoadAssetAsync<Sprite>(location).Task); } var loadedSprites = await Task.WhenAll(tasks); foreach (var sprite in loadedSprites) { AllSprites.Add(sprite.name, sprite); } }
You may call InitSprites
from any async method. For example, you can define a public static async void Load()
method, which you call from the Start()
method in your GameManager
singleton.
Then, inside of that, call await InitSprites("pictures")
. This will asynchronously load and store your sprites wherever you need (in my case, a dictionary).
- Modify
You can get this code to work with any asset type. All you have to do is to replace the typeof()
parameter with your desired type. You can even write typeof(object)
and then store it into a ListList<object>
. That way, one list can hold ALL of your assets of various types, which you can then use however you like.