How to make this struct to reorderable in editor?

Hello,

I’m trying to make a reorderable array/list but I have never made an editor tool before so I’m kind of lost. Can someone help me to finish the script that I started?

Here’s the array I’m trying to make reorderable.

    [SerializeField] private LegendElement[] LegendElements = null;

    [Serializable]
    private struct LegendElement
    {
        public Sprite Icon;
        public Color IconColor;
        public string IconText;
        public Color IconTextColor;
        public string IconName;
    }

Here’s the editor script that I’ve written so far.

using UnityEngine;
using UnityEditor;
using UnityEditorInternal;


[CustomEditor(typeof(Legend))]
public class LegendEditor : Editor
{
    private SerializedProperty Legend = null;
    private ReorderableList List = null;


    private void OnEnable()
    {
        Legend = serializedObject.FindProperty("LegendElement");
   
        List = new ReorderableList(serializedObject, Legend, true, true, true, true);
        List.drawElementCallback = DrawListItems;
        List.drawHeaderCallback = DrawHeader;
    }

    private void DrawListItems(Rect rect, int index, bool isActive, bool isFocused)
    {
        SerializedProperty element = List.serializedProperty.GetArrayElementAtIndex(index);

        // How to show the element in here?
    }

    private void DrawHeader(Rect rect)
    {
        EditorGUI.LabelField(rect, "Legend");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        List.DoLayoutList();
        serializedObject.ApplyModifiedProperties();
    }
}

So far, I’m getting this error in the editor, and the editor doesn’t show anything in the inspector.

Hi @tomang5

If you are looking for serialized property from serialized object, you should be looking it by its name… I think you want to find your LegendElements array - not the type… like it is now:

Legend = serializedObject.FindProperty("LegendElement");

This is how you have named your array:

[SerializeField] private LegendElement[] LegendElements = null;

Didn’t read the code properly so it might be something else too… there are several reorderable list examples, google and compare it to your code.

You are a lifesaver :slight_smile: I was about to give up

1 Like