什么是ReorderableList???
ReorderableList=Re + orderable + List;意思就是可以对List继续重新排序;并且在进行排序的过程中可以进行很多自定义的操作。当然ReorderList也主要是作用于在Editor下面,主要用于在写工具上面,或者Inspector上面做一些自己的需求;
一、首先看一下演示效果
- 添加随机颜色,并且可以看到自定义的标签:颜色数组和【可以自定义别名】
- 删除元素需要确认
3.选择元素进行提示,可以实现对应操作
二、怎么用
- 根据自己的实际情况选择,比如上面的例子就是对TestReorderableList类中colors数组进行操作
- 通过在Editor下实现OnInspectorGUI的重写
- 通过对ReorderableList中需要注册的事件进行注册,然后回调处理自己的实际逻辑
三、代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestReorderableList : MonoBehaviour
{
public List<Color> colors = new List<Color>();
}
- Editor下ReorderableList的功能代码:ReorderableListEditor
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
[CustomEditor(typeof(TestReorderableList))]
public class ReorderableListEditor : Editor
{
private ReorderableList reorderableList;
private void OnEnable()
{
reorderableList = new ReorderableList(serializedObject, serializedObject.FindProperty("colors"), true, true,
true,
true);
reorderableList.drawElementCallback = DrawElementCallback;
reorderableList.drawHeaderCallback = DrawHeaderCallback;
reorderableList.onRemoveCallback = OnRemoveCallback;
reorderableList.onAddCallback = OnAddCallback;
reorderableList.onSelectCallback = OnSelectCallback;
reorderableList.onMouseDragCallback = OnMouseDragCallback;
}
public override void OnInspectorGUI()
{
EditorGUILayout.Space();
serializedObject.Update();
reorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
void DrawElementCallback(Rect rect, int index, bool selected, bool focused)
{
SerializedProperty itemData = reorderableList.serializedProperty.GetArrayElementAtIndex(index);
rect.y += 2;
rect.height = EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(rect, itemData, GUIContent.none);
}
void DrawHeaderCallback(Rect rect)
{
GUI.Label(rect, "颜色数组");
Rect subChildRect = new Rect(rect.x + 80, rect.y, 150, rect.height);
GUI.Label(subChildRect, "【***可以自定义别名***】");
}
void OnRemoveCallback(ReorderableList reorderableList)
{
if (EditorUtility.DisplayDialog("Tips", "Remove The Element???", "ok", "cancel"))
{
ReorderableList.defaultBehaviours.DoRemoveButton(reorderableList);
}
}
void OnAddCallback(ReorderableList reorderableList)
{
if (reorderableList.serializedProperty != null)
{
reorderableList.serializedProperty.arraySize++;
reorderableList.index = reorderableList.serializedProperty.arraySize - 1;
SerializedProperty otherSp = reorderableList.serializedProperty.GetArrayElementAtIndex(0);
SerializedProperty currentSp =
reorderableList.serializedProperty.GetArrayElementAtIndex(reorderableList.index);
currentSp.colorValue = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f));
}
else
{
ReorderableList.defaultBehaviours.DoAddButton(reorderableList);
}
}
void OnSelectCallback(ReorderableList list)
{
EditorUtility.DisplayDialog("提示", "可以对选中的第" + (list.index + 1) + "个Item进行操作", "知道了");
}
void OnMouseDragCallback(ReorderableList list)
{
Debug.Log("元素拖拽");
}
}
结语:-------------------货币无贵贱,BUG无大小–【valaki】
|