Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to use PropertyDrawer to change Array(not its elements) inspector showing name?

Discussion in 'Scripting' started by Middle-earth, Mar 17, 2023.

  1. Middle-earth

    Middle-earth

    Joined:
    Jan 23, 2018
    Posts:
    73
    My code is:
    Code (CSharp):
    1. public class TitleAttribute : PropertyAttribute
    2. {
    3.     public string title;
    4.     public TitleAttribute(string title)
    5.     {
    6.         this.title = title;
    7.     }
    8. }
    9.  
    10.  
    11.  
    12. public class TitleDrawer : PropertyDrawer
    13. {
    14.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    15.     {
    16.         TitleAttribute titleAttribute = attribute as TitleAttribute;
    17.         EditorGUI.PropertyField(position, property, new GUIContent(titleAttribute.title));
    18.     }
    19. }
    20.  
    21.  
    22.  
    23. public class Test : MonoBehaviour
    24. {
    25.     [Title("I want to this name")] public int[] aaa;
    26. }
    27.  
    28.  
    However, the inspector showing name of int[] aaa is not "I want to this name".
    (Actually the elements' inspector showing name become "I want to this name",but I don't care about the elements' name.)
    2023_03_17_19_16.50.jpg

    I just want to use PropertyDrawer to change the array name itself,because aaa is a very ugly name.How the code should be?
     
  2. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    You need a [CustomPropertyDrawer(typeof(TitleAttribute))] on your propertydrawer, so it will be called
     
  3. Middle-earth

    Middle-earth

    Joined:
    Jan 23, 2018
    Posts:
    73
    Maybe not this reason.Already have [CustomPropertyDrawer(typeof(TitleAttribute))] just not show on the screenshot above.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Unfortunately you can not apply PropertyDrawers to the whole array since the array itself is just an organisatorical property which does not contain serialized data. When you apply a PropertyDrawer to an array, it is applied to each element instead. AFAIK there's currently no way to use a PropertyDrawer for arrays / Lists. The only option here would be to either:
    • Use a custom editor for the outer class so you have full control over the whole component.
    • Use a wrapping type for the array / List where you can apply your property drawer / property attribute to.
    So a class like this would allow you to handle this yourself:

    Code (CSharp):
    1. [System.Serializable]
    2. public class WrappedArray<T>
    3. {
    4.     public T[] array;
    5. }

    This would allow you to declare your "array" as

    Code (CSharp):
    1. [Title("I want to this name")]
    2. public WrappedArray<int> myArray;
    Now the title would apply to the WrappedArray class. You may also need / want a PropertyDrawer which handles the drawing of the class itself. PropertyDrawers can be chained, though controlling the order is not that easy.
     
    Middle-earth and spiney199 like this.