Skip to main content
small tweak
Source Link
caleidon
  • 591
  • 8
  • 33

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:

  1. Add your addressables into a group and label them accordingly. In my case, "pictures".

addressables view

  1. 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).

  1. 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.

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:

  1. Add your addressables into a group and label them accordingly. In my case, "pictures".

addressables view

  1. 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).

  1. 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 List. That way, one list can hold ALL of your assets of various types, which you can then use however you like.

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:

  1. Add your addressables into a group and label them accordingly. In my case, "pictures".

addressables view

  1. 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).

  1. 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 List<object>. That way, one list can hold ALL of your assets of various types, which you can then use however you like.

Improved code so it loads ACTUALLY async instead of sequentually and replaced image with actual code
Source Link
caleidon
  • 591
  • 8
  • 33

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:

  1. Add your addressables into a group and label them accordingly. In my case, "pictures".

addressables view

  1. Add the code.

    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);
        }
    }
    

1337 code

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).

  1. 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 List. That way, one list can hold ALL of your assets of various types, which you can then use however you like.

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:

  1. Add your addressables into a group and label them accordingly. In my case, "pictures".

addressables view

  1. Add the code.

1337 code

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).

  1. 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 List. That way, one list can hold ALL of your assets of various types, which you can then use however you like.

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:

  1. Add your addressables into a group and label them accordingly. In my case, "pictures".

addressables view

  1. 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).

  1. 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 List. That way, one list can hold ALL of your assets of various types, which you can then use however you like.

Source Link
caleidon
  • 591
  • 8
  • 33

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:

  1. Add your addressables into a group and label them accordingly. In my case, "pictures".

addressables view

  1. Add the code.

1337 code

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).

  1. 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 List. That way, one list can hold ALL of your assets of various types, which you can then use however you like.