Search Unity

Please make Label Table public

Discussion in 'Addressables' started by Jaimi, Feb 4, 2020.

  1. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,208
    In AddressableAssetSettings, you have a property for the labels "labelTable".

    internal LabelTable labelTable { get { return m_LabelTable; } }

    It would be helpful if you could make this table public, so we could programatically check if labels exist.

    It's a one word change. Would this be possible?
     
    WeltenbauerRenn and Xarbrough like this.
  2. CameronND

    CameronND

    Joined:
    Oct 2, 2018
    Posts:
    89
    I would like this feature too, because it would be useful for me to remove labels via code if they are no longer used.
     
  3. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I'd like to get a list of all defined labels in my editor code. So at least, alongside AddLabel and RemoveLabel, I would love to see GetLabels or similar.

    Here's my use case: I'd like to initialize a custom array of AssetLabelReferences with all available labels. For now, I'm using reflection to retrieve the needed info.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.AddressableAssets;
    3. using System.Reflection;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. [InitializeOnLoad]
    8. public static class AssetLabelPropertyExtension
    9. {
    10.     static AssetLabelPropertyExtension()
    11.     {
    12.         EditorApplication.contextualPropertyMenu += OnPropertyContextMenu;
    13.     }
    14.  
    15.     /// <summary>
    16.     /// Adds a context menu to arrays of AssetLabelReference types which displays
    17.     /// an option to initialize the collection with all available labels.
    18.     /// </summary>
    19.     /// <remarks>
    20.     /// This is useful in cases such as when defining a list of fallback labels to load
    21.     /// or whenever users have to input a list with almost all labels.
    22.     /// </remarks>
    23.     private static void OnPropertyContextMenu(GenericMenu menu, SerializedProperty property)
    24.     {
    25.         if (IsAssetLabelReferenceArray(property))
    26.         {
    27.             // The settings object may not exist yet and so no label exist either.
    28.             var settings = AddressableAssetSettingsDefaultObject.Settings;
    29.             if (settings != null)
    30.             {
    31.                 var copy = property.Copy();
    32.                 menu.AddItem(new GUIContent("Initialize with all labels"), false, () =>
    33.                 {
    34.                     // Since Unity does not expose the list of label, use reflection to find it.
    35.                     var bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
    36.                     var labelTable = settings.GetType().GetProperty("labelTable", bindingFlags).GetValue(settings);
    37.                     var labelNames = (List<string>)labelTable.GetType().GetProperty("labelNames", bindingFlags).GetValue(labelTable);
    38.  
    39.                     // Now we can simply overwrite the array with the found labels.
    40.                     copy.arraySize = labelNames.Count;
    41.                     for (int i = 0; i < labelNames.Count; i++)
    42.                     {
    43.                         var element = copy.GetArrayElementAtIndex(i);
    44.                         var labelStringProp = element.FindPropertyRelative("m_LabelString");
    45.                         labelStringProp.stringValue = labelNames[i];
    46.                     }
    47.  
    48.                     // Undo is available, so no need to prompt the user for this destructive operation.
    49.                     copy.serializedObject.ApplyModifiedProperties();
    50.                 });
    51.             }
    52.         }
    53.     }
    54.  
    55.     private static bool IsAssetLabelReferenceArray(SerializedProperty property)
    56.     {
    57.         return property.propertyType == SerializedPropertyType.Generic
    58.             && property.isArray
    59.             && property.arrayElementType == "AssetLabelReference";
    60.     }
    61. }
     
    Last edited: Apr 9, 2020