Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Arrays in PropertyDrawers

Discussion in 'Immediate Mode GUI (IMGUI)' started by Timbon77, Jun 11, 2021.

  1. Timbon77

    Timbon77

    Joined:
    Oct 3, 2019
    Posts:
    38
    Hello,

    I am having issue for a while now with propertyDrawer.
    I am able to edit all variables that I want in a class that I made except from the array within the class.

    let's say I have the following class:

    Code (CSharp):
    1. public string Name;
    2. Public string Description;
    3. public string[] Items;
    (don't ask why I need the items as a string array it's just an example)

    can someone please just show me how to display an array in the must simple form possible in the inspector?
    that include clicking on the array to get the drop down values/size and edit them.
     
  2. Timbon77

    Timbon77

    Joined:
    Oct 3, 2019
    Posts:
    38
    Anybody? :)
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    "let's say I have the following class:"

    So what exactly is your class? Do you mean that those 3 fields are a serializable C# class?

    "...clicking on the array to get the drop down values/size and edit them."

    What exactly do you mean by this?
     
  4. Timbon77

    Timbon77

    Joined:
    Oct 3, 2019
    Posts:
    38
    "Do you mean that those 3 fields are a serializable C# class?" - yes
    And what I mean is that I want to display an array in the must simple form, just as if I wouldn't use Property drawer, the name of the array appear, you click on it and and you have a size field (size of array) and values.
    In other words, to be able to show/edit all variables of the defined array.
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    You can draw lists and arrays in property drawers like this, simply set the second parameter true:

    Code (CSharp):
    1.  EditorGUI.PropertyField(myrect, property.FindPropertyRelative("items"), null, true);
    To make lists render correctly, you also probably have to tweak indent, label width and rect width IIRC.

    And you will also have to use GetPropertyHeight to make your row height adapt to your rendered list height.

    Something like this. It doesn't look exactly like the default list because the label is set narrower:

    20210613_property_drawer.gif
     
    Last edited: Jun 13, 2021
  6. Timbon77

    Timbon77

    Joined:
    Oct 3, 2019
    Posts:
    38
    That looks about right that's the example that I was looking for, thanks!