Apologies if this has been requested or discussed before.
I would like to see the addition of an overload to the LoadAsset family of methods, that adds a Type parameter rather than just the generic versions we have now.
For example, the new signature would be:
AsyncOperationHandle LoadAssetAsync(object key, Type type)
The reason why I would find this useful is when using addressables to load assets based on a kind of config file, where I specify the addressable path and also it’s C# type, something like
{
name = Soldier
hitpoints = 100
icon = sprites/my_sprite.png
}
Yes, I know this is quite niche and yes, I know that there are quite a few workarounds. I could use reflection to invoke the generic method, I could get a list of all resource locations and select the right one… But I feel like I shouldn’t have to, especially since internally the generic method just does this:
var t = typeof(TObject);
if (t.IsArray)
t = t.GetElementType();
// ...
And no, I can’t just do:
(Sprite)Addressables.LoadAssetAsync<UnityEngine.Object>("sprites/my_sprite.png")
because using Object as the parameter will cause it to be loaded as a Texture2D, which can’t be cast to a Sprite. There are other instances where an asset can exist as multiple types, so this is not a solution unfortunately.
I feel like this change to the API would bring it in line with other Unity generic methods, such as GetComponent<T>()
which has the equivalent
GetComponent(Type t)
Thanks for the consideration.
If anyone knows of a workaround that avoids reflection, or having to load all addresses, and only loads the exact asset of the exact type I want, please let me know.