【第三问 Unity中ReorderableList怎么用?】

什么是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】

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值