首页 > 其他分享 >ReorderableList

ReorderableList

时间:2024-10-15 11:00:28浏览次数:7  
标签:idx list 列表 ReorderableList element true reorderableList

目录

简介

列表重写的数据类,一般使用在Editor下

常用委托事件

委托名 作用
drawElementCallback 用于绘制list里的每个element
drawHeaderCallback 用于绘制header
elementHeightCallback 用于设置每个element的高度
onAddCallback element添加时的回调
onAddDropdownCallback +号点击后可出现列表的回调
onRemoveCallback element移除时的回调

代码示例

//将对应的列表转换为ReorderableList
reorderableList = new ReorderableList(serializedObject, serializedObject.FindProperty("moduleList"), true, true, true, true);

// 自定义DrawElement回调函数,控制列表每一项的显示
reorderableList.drawElementCallback = (Rect rect, int idx, bool isActive, bool isFocused) =>
{
    var element = reorderableList.serializedProperty.GetArrayElementAtIndex(idx).objectReferenceValue as BaseAudioModule;

    EditorGUI.LabelField(rect, element.name);
};

//列表添加,需要点击加号时出现列表
reorderableList.onAddDropdownCallback = (rect,list) =>
{
    GenericMenu menu = new GenericMenu();

    //添加选项到下拉菜单
    menu.AddItem(new GUIContent("UIModule"), false, AddUIModule);
    menu.AddItem(new GUIContent("FadeModule"), false, AddFadeModule);

    menu.ShowAsContext();
};
//列表移除,需要点击减号时出现提示或者支持撤销
reorderableList.onRemoveCallback = (list) =>
{
    if (EditorUtility.DisplayDialog("提示", "确认移除音效模块", "确定", "取消"))
    {
        int idx = list.index;
        if (idx >= 0 && idx < list.count)
        {
            //移除
            var property = list.serializedProperty.GetArrayElementAtIndex(idx).objectReferenceValue as BaseAudioModule;
            //myTarget.Remove(property);
            DestroyImmediate(property, true);
            list.serializedProperty.DeleteArrayElementAtIndex(idx);
            serializedObject.ApplyModifiedProperties();
            AssetDatabase.ImportAsset(path);
            AssetDatabase.Refresh();
        }
        EditorUtility.SetDirty(target);
    }
};

标签:idx,list,列表,ReorderableList,element,true,reorderableList
From: https://www.cnblogs.com/comradexiao/p/18467011

相关文章