Working with arrays as SerializedProperty(ies) in editor script

To get nice custom editors for arrays/lists, Unity has an internal class called ReorderableList. I’m still playing around with it, you can learn more on it here:
Here is a blog post explaining how to use it:

Here is a github repo that seems to make it easily accessible:

I’m still figuring this out myself, but this seems to be the list drawer that is used in many assets.

I can’t seem to get rid of the “Element 0, Element 1,…” labels in the inspector.

Is is possible?

The expand arrow icon thing has been removed but not the element labels.

I’ve attached a screenshot of the result and here is the inspector:

public class SomeContainerEditor : Editor
    {
        SerializedProperty someArrayProperty;

        public void OnEnable()
        {
            someArrayProperty = serializedObject.FindProperty("someArrayProperty");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            for (int i = 0; i < someArrayProperty.arraySize; i++)
            {
                var someArrayItem= someArrayProperty.GetArrayElementAtIndex(i);

                EditorGUILayout.PropertyField(someArrayItem);
            }

            serializedObject.ApplyModifiedProperties();
        }
}

6119663--666767--upload_2020-7-22_23-52-51.png

As you can see above, it’s an array of a custom Type which I’ve created a property drawer for. The property drawer seems to be working as intended.

I’m assuming there is some sort of information that gets serialized in the someArrayItem property that indicates that it’s part of an array and not just a plain instance?

I changed EditorGUILayout.PropertyField(controllerStringProperty); to EditorGUILayout.PropertyField(controllerStringProperty, GUIContent.none); works like a charm

2 Likes

This thread helped me a lot. Here’s a struct that encapsulates access to an array property. Works for updating enum values in bulk, but can be adjusted to handle any other types.

#if UNITY_EDITOR
using System;
using UnityEditor;

public struct ArraySerializedProperty {
    private SerializedObject _serializedObject;
    private string _name;

    public void SetLength(int size) {
        if (size == Length()) return;
        GetArraySizeProperty().intValue = size;
    }

    public int Length() {
        return GetArraySizeProperty().intValue;
    }

    public void SetEnumIntValue(int i, int enumValueIndex) {
        GetArrayElementProperty(i).enumValueIndex = enumValueIndex;
    }

    public void UpdateEnumArray<T>(T[] values) where T : Enum {
        if (Length() != values.Length) {
            SetLength(values.Length);
        }

        for (var i = 0; i < values.Length; i++) {
            var value = values[i];
            var enumValueIndex = (int)(object)value;
            SetEnumIntValue(i, enumValueIndex);
        }
    }

    private SerializedProperty GetArraySizeProperty() {
        return _serializedObject.FindProperty($"{_name}.Array.size");
    }

    private SerializedProperty GetArrayElementProperty(int i) {
        return _serializedObject.FindProperty($"{_name}.Array.data[{i}]");
    }

    public static ArraySerializedProperty Create(SerializedObject serializedObject, string name) {
        return new ArraySerializedProperty {
            _name = name,
            _serializedObject = serializedObject
        };
    }
}
#endif

Also can be found in this gist: ArraySerializedProperty · GitHub

Usage:

SomeEnum[] values = ....
var  someEnumArrayProperty = ArraySerializedProperty.Create(serializedObject, "_someEnumArray");
someEnumArrayPropery.UpdateEnumArray(values);
1 Like