Unity编辑器拓展之一:ReorderableList可重新排序的列表框(简单使用)

可重新排序的列表框

先看效果图:
这里写图片描述

UnityEditorInternal命名空间下提供 一个类ReorderableList可以实现通过拖曳来达到列表元素的重新排序。

基本使用:
名称 描述
draggable 拖曳排序
displayAdd 显示添加按钮
displayRemove 显示移除按钮
elementHeight 元素高度
headerHeight 表头高度
footerHeight 尾部高度
showDefaultBackground 显示默认背景
drawHeaderCallback 绘制表头回调
drawFooterCallback 绘制尾部回调
drawElementCallback 绘制元素回调
drawElementBackgroundCallback 绘制元素背景回调
onReorderCallback 重新排序回调
onSelectCallback 选中回调
onAddCallback 添加按钮回调
onAddDropdownCallback 添加下拉选项回调
onRemoveCallback 移除元素回调
onMouseUpCallback 鼠标抬起回调
onCanRemoveCallback 是否显示可移除按钮回调
onChangedCallback 列表改变回调

示例代码如下:
先创建一个脚本:TestList

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

public class TestList : MonoBehaviour 
{
    public List<Color> m_colors = new List<Color>();

    private void Start()
    {

    }
}

上述代码中有一个List类型的变量,接下来使用一个继承自Editor脚本实现主要逻辑

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

[CustomEditor(typeof(TestList))]
public class TestListEditor : Editor 
{
    private ReorderableList m_colors;
    private void OnEnable()
    {
        m_colors = new ReorderableList(serializedObject, serializedObject.FindProperty("m_colors"), true, true, true, true);
        //绘制元素
        m_colors.drawElementCallback = (Rect rect, int index, bool selected, bool focused) =>
        {
            SerializedProperty itemData = m_colors.serializedProperty.GetArrayElementAtIndex(index);
            rect.y += 2;
            rect.height = EditorGUIUtility.singleLineHeight;
            EditorGUI.PropertyField(rect, itemData, GUIContent.none);
        };
        //绘制表头
        m_colors.drawHeaderCallback = (Rect rect) =>
        {
            GUI.Label(rect, "Colors");
        };
        //当移除元素时回调
        m_colors.onRemoveCallback = (ReorderableList list) =>
        {
            //弹出一个对话框
            if (EditorUtility.DisplayDialog("警告","是否确定删除该颜色","是","否"))
            {
                //当点击“是”
                ReorderableList.defaultBehaviours.DoRemoveButton(list);
            }
        };
        //添加按钮回调
        m_colors.onAddCallback = (ReorderableList list) =>
        {
            if (list.serializedProperty != null)
            {
                list.serializedProperty.arraySize++;
                list.index = list.serializedProperty.arraySize - 1;
                SerializedProperty itemData = list.serializedProperty.GetArrayElementAtIndex(list.index);
                itemData.colorValue = Color.red;
            }
            else
            {
                ReorderableList.defaultBehaviours.DoAddButton(list);
            }
        };
        //鼠标抬起回调
        m_colors.onMouseUpCallback = (ReorderableList list) =>
        {
            Debug.Log("MouseUP");
        };
        //当选择元素回调
        m_colors.onSelectCallback = (ReorderableList list) =>
        {
            //打印选中元素的索引
            Debug.Log(list.index);
        };
    }

    public override void OnInspectorGUI()
    {
        EditorGUILayout.Space();
        serializedObject.Update();
        m_colors.DoLayoutList();
        serializedObject.ApplyModifiedProperties();
    }
}

结果就如之前的图中显示所示。

其中OnInspectorGUI函数中,删除了base.OnInspectorGUI();
不然的话,效果如下图:
这里写图片描述

可以看出,colors绘制了两遍。

示例工程链接:
链接:http://pan.baidu.com/s/1o7Tjslc 密码:pgri

以上知识分享,如有错误,欢迎指出,共同学习,共同进步

猜你喜欢

转载自blog.csdn.net/qq_26999509/article/details/77782177
今日推荐