Search Unity

How to load asset with IResourceLocation and ResourceType, reflection?

Discussion in 'Addressables' started by Favo-Yang, Jun 19, 2019.

  1. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    308
    With version 1.1.3, ResourceType was added to IResourceLocation, to allow us know the type of a location before loading it. Then probably next step is going to do something like this,

    Code (CSharp):
    1. if (loc.ResourceType == typeof(Sprite)
    2. {
    3.     // Special load logic
    4. } else
    5. {
    6.     // Fallback to default LoadAssetAsync
    7.     // However, how to invoke LoadAssetAsync<T> with ResourceType
    8. }
    9.  
    It seems reflection is needed, something like

    Code (CSharp):
    1. var mi = typeof(Addressables).GetMethod("LoadAssetAsync");
    2. var method = mi.MakeGenericMethod(new[] { loc.ResourceType });
    3. dynamic ret = method.Invoke(null, new[] { loc });
    4. AsyncOperationHandle handle = ret.Value;
    5. var asset = await handle.Task;
    Would be clear to have?

    Code (CSharp):
    1. AsyncOperationHandle Addressables.LoadAssetAsync(string loc, Type type);
    Thought?