UnityEditor 编辑器扩展 ReorderableList 可排序列表
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();
}
}