Unity默认对List与Array的序列化,在Inspector面板上的显示,对操作并不是很友好,比如需要交换两个数组元素下标时,需要挨个赋值两个被交换的元素,当数组元素为复杂的类对象时,此操作更加麻烦,而Reorderable List可以更加直观的显示数组元素,整体上会更加美观,并且自带拖拽等操作,以下使用案例截取Cinemachine插件使用Reorderable List的部分代码
声明
private ReorderableList mWaypointList;
初始化
void SetupWaypointList() { mWaypointList = new ReorderableList( serializedObject, FindProperty(x => x.m_Waypoints), true, true, true, true); mWaypointList.elementHeight *= 3; mWaypointList.drawHeaderCallback = (Rect rect) => { EditorGUI.LabelField(rect, "Waypoints"); }; mWaypointList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { DrawWaypointEditor(rect, index); }; mWaypointList.onAddCallback = (ReorderableList l) => { InsertWaypointAtIndex(l.index); }; }
最后在OnInspectorGUI函数中调用
mWaypointList.DoLayoutList();
效果如下
Reorderable List是通过回调函数进行绘制的,代码简单不做阐述,总之接口很全面,转到定义看描述即可,Reorderable List可以配合PropertyDrawer完成常规列表的统一样式修改
文章评论