什么是ReorderableList???
ReorderableList=Re + orderable + List;意思就是可以对List继续重新排序;并且在进行排序的过程中可以进行很多自定义的操作。当然ReorderList也主要是作用于在Editor下面,主要用于在写工具上面,或者Inspector上面做一些自己的需求;
一、首先看一下演示效果
- 添加随机颜色,并且可以看到自定义的标签:颜色数组和【可以自定义别名】
- 删除元素需要确认
3.选择元素进行提示,可以实现对应操作
二、怎么用
- 根据自己的实际情况选择,比如上面的例子就是对TestReorderableList类中colors数组进行操作
- 通过在Editor下实现OnInspectorGUI的重写
- 通过对ReorderableList中需要注册的事件进行注册,然后回调处理自己的实际逻辑
三、代码
- 组件代码TestReorderableList
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;
//绘制Header回调
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();
}
/// <summary>
/// 会在元素回调
/// </summary>
/// <param name="rect"></param>
/// <param name="index"></param>
/// <param name="selected"></param>
/// <param name="focused"></param>
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 = otherSp.colorValue;
//随机一个元素值
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】