Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

How to load assets in editor script?

Discussion in 'Addressables' started by zyc_dc, Aug 11, 2018.

  1. zyc_dc

    zyc_dc

    Joined:
    May 11, 2018
    Posts:
    42
    Hi,

    I got an editor which could work without clicking the Play button. Because there is only async load in the Addressables system, it seems there is no way to load assets in editor mode now. I've tried something like unity-editor-coroutine, but it did not work.
     
    Xarbrough likes this.
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    5,454
    We don't currently support editor loading. If you want to load something in Editor just use the normal AssetDatabase like so

    Code (CSharp):
    1. var aaSettings = AddressableAssetSettings.GetDefault(true, true);
    2. var entry = aaSettings.FindAssetEntry("My Asset");
    3. var asset = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(entry.guid);
     
    FlightOfOne and funbites like this.
  3. zyc_dc

    zyc_dc

    Joined:
    May 11, 2018
    Posts:
    42
    Thanks. This workaround seems good.
     
  4. zyc_dc

    zyc_dc

    Joined:
    May 11, 2018
    Posts:
    42
    I tried the API, but the parameter of FindAssetEntry is guid. It is not the name of the asset.
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    5,454
    Ok you can just get all the assets with `GatherAllAssets` and then iterate through looking for the name.
     
  6. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    325
    Seems this code is out dated.

    @karl_jones Have you a new way to load addressables for editor script ?
     
    Last edited: May 2, 2019
  7. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    325
    Found new API

    Replace

    Code (CSharp):
    1. var aaSettings = AddressableAssetSettings.GetDefault(true, true)
    with

    Code (CSharp):
    1. AddressableAssetSettingsDefaultObject.GetSettings(true)
    Adn for the "GatherAllAssets" from @karl_jones, do

    Code (CSharp):
    1.                 var assetsList = new List<AddressableAssetEntry>(1024);
    2.                 AddressableAssetSettingsDefaultObject.GetSettings(false).GetAllAssets(assetsList);
    3.  
    An example or a better API for find assets in editor UI could be great guys.
     
    funbites and karl_jones like this.
  8. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    325
    I write this helper, it's not perfect with subassets but that works at least

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEditor;
    4. using UnityEditor.AddressableAssets;
    5. using UnityEditor.AddressableAssets.Settings;
    6. using UnityEngine;
    7.  
    8. namespace HMH.Editor
    9. {
    10. #if UNITY_EDITOR
    11.     [InitializeOnLoad]
    12.     public static class AddressableHelper
    13.     {
    14.         static AddressableHelper()
    15.         {
    16.             var assetsList = new List<AddressableAssetEntry>(2048);
    17.             AddressableAssetSettingsDefaultObject.GetSettings(true).GetAllAssets(assetsList);
    18.  
    19.             foreach (var entry in assetsList)
    20.                 AddEntry(entry);
    21.  
    22.             AddressableAssetSettings.OnModificationGlobal += AddressableAssetSettingsOnOnModificationGlobal;
    23.         }
    24.  
    25.         private static void AddressableAssetSettingsOnOnModificationGlobal(AddressableAssetSettings settings, AddressableAssetSettings.ModificationEvent modificationType, object entry)
    26.         {
    27.             var listEntries = entry as List<AddressableAssetEntry>;
    28.  
    29.             if (listEntries == null)
    30.                 switch (modificationType)
    31.                 {
    32.                     case AddressableAssetSettings.ModificationEvent.EntryAdded:
    33.                     case AddressableAssetSettings.ModificationEvent.EntryCreated:
    34.                         AddEntry((AddressableAssetEntry)entry);
    35.  
    36.                         break;
    37.                     case AddressableAssetSettings.ModificationEvent.EntryRemoved:
    38.                         var addressableEntryRemoved = (AddressableAssetEntry)entry;
    39.  
    40.                         if (_addressesToEntry.TryGetValue(addressableEntryRemoved.address, out var entryList))
    41.                             entryList.Remove(addressableEntryRemoved);
    42.  
    43.                         break;
    44.                 }
    45.             else
    46.                 switch (modificationType)
    47.                 {
    48.                     case AddressableAssetSettings.ModificationEvent.EntryAdded:
    49.                     case AddressableAssetSettings.ModificationEvent.EntryCreated:
    50.  
    51.                         foreach (var entryItem in listEntries)
    52.                             AddEntry(entryItem);
    53.  
    54.                         break;
    55.                     case AddressableAssetSettings.ModificationEvent.EntryRemoved:
    56.  
    57.                         foreach (var entryItem in listEntries)
    58.                             if (_addressesToEntry.TryGetValue(entryItem.address, out var entryList))
    59.                                 entryList.Remove(entryItem);
    60.  
    61.                         break;
    62.                 }
    63.         }
    64.  
    65.         private static void AddEntry(AddressableAssetEntry entry)
    66.         {
    67.             if (_addressesToEntry.TryGetValue(entry.address, out var entryList) == false)
    68.             {
    69.                 entryList = new List<AddressableAssetEntry>(1);
    70.                 _addressesToEntry.Add(entry.address, entryList);
    71.             }
    72.  
    73.             entryList.Add(entry);
    74.         }
    75.  
    76.         public static List<AddressableAssetEntry> FindEntry<T>(string address) where T : Object
    77.         {
    78.             if (_addressesToEntry.TryGetValue(address, out var entryList) == false)
    79.                 return null;
    80.  
    81.             var foundList = new List<AddressableAssetEntry>();
    82.  
    83.             foreach (var entry in entryList)
    84.             {
    85.                 var p = entry.AssetPath;
    86.  
    87.                 if (AssetDatabase.GetMainAssetTypeAtPath(p) == typeof(T))
    88.                     foundList.Add(entry);
    89.             }
    90.  
    91.             return foundList;
    92.         }
    93.  
    94.         public static T FindFirstEntry<T>(string address) where T : Object
    95.         {
    96.             var entries = FindEntry<T>(address);
    97.  
    98.             if (entries == null || entries.Count < 1)
    99.                 return null;
    100.  
    101.             return AssetDatabase.LoadAssetAtPath<T>(entries[0].AssetPath);
    102.         }
    103.  
    104.         #region Variables
    105.  
    106.         private static Dictionary<string, List<AddressableAssetEntry>> _addressesToEntry = new Dictionary<string, List<AddressableAssetEntry>>(2048);
    107.  
    108.         #endregion
    109.     }
    110.     #endif
    111.  
    112. }
    113.  
     
    Last edited: May 12, 2019
    jeanluch_playtika and simonsen like this.
  9. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    249
    Be careful! This will break your Addressables Settings if you update your unity version or share your project excluding the library folder (when using Version Control for example).

    I changed the Constructor to be initialized by myself and not on load, if you do that it's shareable.
     
    Last edited: Jun 24, 2020
unityunity