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);
}
};
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)