Search Unity

If you use AddressableAsset to load resources in editor mode?

Discussion in 'Addressables' started by zhuxianzhi, Nov 7, 2018.

  1. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    22
    I want to use a consistent API, either in editor mode or in play mode. The code looks like this. But adding a folder addressing, I can't find the correct AssetPath.

    Code (CSharp):
    1.    private static async Task<T> LoadAsset<T>(string key) where T : UnityEngine.Object
    2.     {
    3.         if (Application.isPlaying)
    4.         {
    5.             var iAsyncOperation = Addressables.LoadAsset<T>(key);
    6.             await iAsyncOperation;
    7.             return iAsyncOperation.Result;
    8.         }
    9.         else
    10.         {
    11.             var aaSettings = AddressableAssetSettingsDefaultObject.Settings;
    12.             var entries = from addressableAssetGroup in aaSettings.groups
    13.                 from entrie in addressableAssetGroup.entries
    14.                 where entrie.AssetPath == key || entrie.address == key
    15.                 select entrie;
    16.  
    17.             foreach (var entrie in entries)
    18.             {
    19.                 return AssetDatabase.LoadAssetAtPath<T>(entrie.AssetPath);
    20.             }
    21.             return default;
    22.         }
    23.     }
     
  2. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,265
    If you are able to use AssetReference, then you can use `.editorAsset`. If not, then it is inevitable that you have to provide separated path in editor. I have some helper methods with intention like yours

    Code (CSharp):
    1. public static class AddressablesExtension
    2. {
    3.     public static bool IsNullOrEmpty(this AssetReference aref)
    4.     {
    5.         return aref == null || aref.RuntimeKey == Hash128.Parse("");
    6.     }
    7.  
    8.     /// <summary>
    9.     /// Use the Addressables system if in real play, use `editorAsset` if in edit mode.
    10.     /// </summary>
    11.     public static async UniTask<T> LoadAssetX<T>(this AssetReference aref)
    12.     where T : UnityEngine.Object
    13.     {
    14. #if UNITY_EDITOR
    15.         if (!Application.isPlaying)
    16.         {
    17.             return (T)aref.editorAsset;
    18.         }
    19. #endif
    20.         var op = aref.LoadAsset<T>();
    21.         var result = await op;
    22.         //Debug.Log($"{op.Status} {object.ReferenceEquals(null, op.Result)} {op.IsDone} {op.IsValid} {op.OperationException}");
    23.         return result;
    24.     }
    25.  
    26.     /// <summary>
    27.     /// Use the Addressables system if in real play, use `AssetDatabase` if in edit mode.
    28.     /// </summary>
    29.     /// <param name="key">Addressable key</param>
    30.     /// <param name="pathForEditor">This starts with "Assets/..." and you need the file extension as well.</param>
    31.     public static async UniTask<T> LoadAssetX<T>(string key, string pathForEditor)
    32.     where T : UnityEngine.Object
    33.     {
    34. #if UNITY_EDITOR
    35.         if (!Application.isPlaying)
    36.         {
    37.             return AssetDatabase.LoadAssetAtPath<T>(pathForEditor);
    38.         }
    39. #endif
    40.         return await Addressables.LoadAsset<T>(key);
    41.     }
    42. }
     
    SugoiDev likes this.
  3. tencnivel

    tencnivel

    Joined:
    Sep 26, 2017
    Posts:
    11
    I was also using UniRX but as of today it is not compatible with Unity 2019.X.X

    Here is my version (tested with Addressables System V0.8.4 ):


    Code (CSharp):
    1.  private static async Task<T> LoadAsset<T>(string key) where T : UnityEngine.Object
    2.     {
    3.      
    4.         if (Application.isPlaying)
    5.         {
    6.             AsyncOperationHandle<T> handle = Addressables.LoadAssetAsync<T>(key);
    7.  
    8.             await handle.Task; // wait for the task to complete before we try to get the result
    9.  
    10.             T result = (T)handle.Result;
    11.             // Addressables.Release(handle); // DO NOT release the handle! that would make the loaded asset null when in virtual mode or packed mode
    12.  
    13.             return result;
    14.  
    15.         }
    16.         else
    17.         {
    18.             return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(key);
    19.        
    20.         }
    21.     }
     
    Last edited: May 25, 2019