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

博客迁移

个人博客站点,欢迎访问,www.jiingfengji.tech

可重新排序的列表框

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

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

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值