kingBook

导航

UnityEditor 编辑器扩展 ReorderableList 可排序列表

image

TestReorderableList.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestReorderableList : MonoBehaviour {

    [SerializeField] private List<string> m_texts;
    //[SerializeField] private string[] m_texts;

}

EditorTestReorderableList.cs 放置在 Editor 文件夹下:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(TestReorderableList))]
public class EditorTestReorderableList : Editor {

    private ReorderableList m_reorderableList;

    private void OnEnable () {
        SerializedProperty prop = serializedObject.FindProperty("m_texts");
        m_reorderableList = new ReorderableList(serializedObject, prop);

        m_reorderableList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => {
            var element = prop.GetArrayElementAtIndex(index);
            EditorGUI.PropertyField(rect, element);
        };

        m_reorderableList.drawHeaderCallback = (Rect rect) => {
            EditorGUI.LabelField(rect, prop.displayName);
        };
    }

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

posted on 2022-02-21 20:45  kingBook  阅读(556)  评论(0编辑  收藏  举报