x


Editor: How to do PropertyField for List elements?

Hi there,

I finally got a array handling with serialized properties working, but I need the same for generic lists. It comes down to to the question of "How to access List.Count and List[dataindex] when using FindProperty()" ???

Here's my working array code so far:

void ArrayGUI(SerializedObject obj,string name)
    {
        int no = obj.FindProperty(name + ".Array.size").intValue;
        EditorGUI.indentLevel = 3;
        int c = EditorGUILayout.IntField("Size", no);
        if (c != no)
            obj.FindProperty(name + ".Array.size").intValue = c;

        for (int i=0;i<no;i++) {
            var prop = obj.FindProperty(string.Format("{0}.Array.data[{1}]", name, i));
            EditorGUILayout.PropertyField(prop);
        }
    }

Thanks in advance

Jake

more ▼

asked Dec 30 '11 at 09:31 AM

Jake L. gravatar image

Jake L.
943 5 6 22

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

DrawDefaultInpector() does something like this:

override def OnInspectorGUI():
    serializedObject.Update()

    EditorGUIUtility.LookLikeInspector()

    myIterator = serializedObject.FindProperty("myArrayField")
    while true:
        myRect = GUILayoutUtility.GetRect(0f, 16f)
        showChildren = EditorGUI.PropertyField(myRect, myIterator)
        break unless myIterator.NextVisible(showChildren)

    serializedObject.ApplyModifiedProperties()
more ▼

answered Feb 24 '12 at 02:04 PM

steinbitglis gravatar image

steinbitglis
259 12 16 19

Thanks a lot for answering this. I don't have the time to test this in the near future, but I'll assume this will do the trick, so I'll flag this answer as correct.

Thanks mate!

Feb 24 '12 at 02:44 PM Jake L.

Thanks, very usefull !

I can't find this on Unity Documentation.

Jun 28 '12 at 09:53 AM watermy
(comments are locked)
10|3000 characters needed characters left

C# version of steinbitglis answered

public override void OnInspectorGUI()
{
    serializedObject.Update();
    EditorGUIUtility.LookLikeInspector();
    ListIterator("myArrayField");
    serializedObject.ApplyModifiedProperties();
}

public void ListIterator(string listName)
    {
        //List object
        SerializedProperty listIterator = serializedObject.FindProperty(listName);
        Rect drawZone = GUILayoutUtility.GetRect(0f, 16f);
        bool showChildren = EditorGUI.PropertyField(drawZone, listIterator);
        listIterator.NextVisible(showChildren);

        //List size
        drawZone = GUILayoutUtility.GetRect(0f, 16f);
        showChildren = EditorGUI.PropertyField(drawZone, listIterator);
        bool toBeContinued = listIterator.NextVisible(showChildren);

        //Elements
        int listElement = 0;
        while (toBeContinued)
        {
            drawZone = GUILayoutUtility.GetRect(0f, 16f);
            showChildren = EditorGUI.PropertyField(drawZone, listIterator);
            toBeContinued = listIterator.NextVisible(showChildren);
            listElement++;
        }

}

Update : I encountered problem with expending and nested list (like Unity crash >_<)

So here a new version which work for me.

 private listVisibility = true;

...
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUIUtility.LookLikeInspector();
        ListIterator("myArrayField", ref listVisibility);
        serializedObject.ApplyModifiedProperties();
    }


 public void ListIterator(string propertyPath, ref bool visible)
    {
        SerializedProperty listProperty = serializedObject.FindProperty(propertyPath);
        visible = EditorGUILayout.Foldout(visible, listProperty.name);
        if (visible)
        {
            EditorGUI.indentLevel++;
            for (int i = 0; i < listProperty.arraySize; i++)
            {
                SerializedProperty elementProperty = listProperty.GetArrayElementAtIndex(i);
                Rect drawZone = GUILayoutUtility.GetRect(0f, 16f);
                bool showChildren = EditorGUI.PropertyField(drawZone, elementProperty); 
            }
            EditorGUI.indentLevel--;
        }
    }
more ▼

answered Jun 28 '12 at 10:25 AM

watermy gravatar image

watermy
200 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1665
x355
x30
x18
x13

asked: Dec 30 '11 at 09:31 AM

Seen: 2295 times

Last Updated: Jun 28 '12 at 01:11 PM