Unity编辑器拓展之一:ReorderableList可重新排序的列表框(简单使用)
来源:互联网 发布:linux 删除tomcat日志 编辑:程序博客网 时间:2024/06/01 22:32
可重新排序的列表框
先看效果图:
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
以上知识分享,如有错误,欢迎指出,共同学习,共同进步
- Unity编辑器拓展之一:ReorderableList可重新排序的列表框(简单使用)
- Unity编辑器拓展之二:ReorderableList可重新排序的列表框(复杂使用)
- Unity ReorderableList 可重新排序的列表框使用
- 【unity拓展】使列表支持拖动排序(Reorderablelist)
- Unity Editor 编辑器扩展 十一 Inspector可排序列表
- Unity编辑器拓展之三:拓展Unity的Hierarchy面板
- 【小松教你手游开发】【unity实用技能】拓展函数(给系统代码添加可直接使用的接口)
- unity编辑器拓展
- Unity编辑器拓展<一>
- Unity编辑器拓展<二>
- unity拓展编辑器
- unity编辑器拓展一——删除项目工程里的未使用Shader
- 【unity编辑器拓展】使用脚本添加Prefab到场景中
- Unity的编辑器使用
- Unity编辑器的使用
- 【Unity编辑器扩展简介】在MonoBehaviour中可使用的编辑器扩展Attribute
- ExpandableListView可扩展的下拉列表简单的使用
- ReorderableList
- Invitation Cards
- Mybatis物理分页插件PageHelper 5.0
- 不再是一个学生
- 【HTML5学习笔记】33:CSS3过渡效果
- 病闲细笔
- Unity编辑器拓展之一:ReorderableList可重新排序的列表框(简单使用)
- bootstrap 利用jquery 添加disabled属性
- Oracle 用hint来影响执行计划
- Javaweb基础之cookie&session
- ionic2+启动白屏问题-------之补充解决之道
- Subway
- 信号在android源码/external/dhcpcd 源码项目中的应用解读分析
- (13)会话和cookie技术
- 正则表达式