Search Unity

[Suggestion] reflective asset

Discussion in 'Addressables' started by Favo-Yang, May 31, 2019.

  1. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    268
    It's probably not an accurate word, "reflective" asset here means to retrieve extra meta data of asset, like name, path, labels and particularly the asset type. In API level, it means two things
    • LoadAssetAsync could aware the type of asset without providing TObject.
    • Extra meta data list above, could be retrieved via the ResourceLocation object.

    Addressables.LoadAssetAsync<T> asks the invoker to provide the TObject type, which seems unnecessary. The addressables system already know the type of asset during build time, either it's a prefab (GameObject), sprite, texture, or text asset.

    In the load-on-demand scenario, it's totally okay. The invoker should know the type of asset. But in the preload-everything scenario, where all assets are being load and cached in an asset holder object to serve further use, It's harder to implement. For example, without reflective asset, The holder has to aware each asset type, and load them with different LoadAssetAsync<TObject> calls. With reflective asset, we can just mark all assets with one label, then load it once.

    Code (CSharp):
    1. // Without reflective asset.
    2.  
    3. List<AssetLabelReference> spriteLabels;
    4. List<AssetLabelReference> prefabLabels;
    5. List<AssetLabelReference> textAssetLabels;
    6. ....
    7. foreach (var labelRef in spriteLabels) {
    8.     var locs = await Addressables.LoadAssetAsync(labelRef.labelString).Task;
    9.     var sprites = await Addressables.LoadAssetsAsync<Sprite>(locs).Task;
    10.     // cache the sprites, by storing to a dictionary(path => sprite asset).
    11. }
    12. // REPEAT for other types...
    13.  
    Code (CSharp):
    1. // With reflective asset.
    2.  
    3. List<AssetLabelReference> labels;
    4. ....
    5. foreach (var labelRef in labels) {
    6.     var locs = await Addressables.LoadAssetAsync(labelRef.labelString).Task;
    7.     // If LoadAssetsAsync can return the desired asset type, then
    8.     var assets = await Addressables.LoadAssetsAsync(locs).Task;
    9.     // Otherwise if the location object knows the type of asset, at least we can do
    10.     foreach (var loc in locs) {
    11.         var assetType = loc.GetAssetType();
    12.         // Load asset by different assetType
    13.         ....
    14.     }
    15. }
    16.  
    @unity_bill
     
  2. Favo-Yang

    Favo-Yang

    Joined:
    Apr 4, 2011
    Posts:
    268
    One more thing,I wonder why Addressables.Release<T> need to be a generic method? Any difference under the hood?

    Code (csharp):
    1. Addressables.Release(sprite)
    2. Addressables.Release(sprite as Object).