Unity编辑器拓展之二:ReorderableList可重新排序的列表框(复杂使用)
来源:互联网 发布:广州女人街网络批发 编辑:程序博客网 时间:2024/06/04 19:18
先看效果gif图:
如果没有看过Unity编辑器拓展之一:ReorderableList可重新排序的列表框(简单使用)的,可以先看这一篇:
http://blog.csdn.net/qq_26999509/article/details/77782177
在此基础上,来绘制更加复杂的类
先提供一个需要绘制的类
using UnityEngine;using System.Collections;using System.Collections.Generic;using System;public class CharacterTest : MonoBehaviour { public List<Character> characters = new List<Character>(); // Use this for initialization void Start () { } // Update is called once per frame void Update () { }}[Serializable]public class Character{ [SerializeField] Texture icon; [SerializeField] string name; [SerializeField] int hp; [SerializeField] int power; [SerializeField] GameObject weapon;}
然后跟上一篇一样,通过使用ReorderableList绘制List
using UnityEngine;using System.Collections;using UnityEditor;using UnityEditorInternal;[CustomEditor(typeof(CharacterTest))]public class CharacterInspector : Editor { ReorderableList reorderableList; void OnEnable() { SerializedProperty prop = serializedObject.FindProperty("characters"); reorderableList = new ReorderableList(serializedObject, prop, true, true, true, true); //设置单个元素的高度 reorderableList.elementHeight = 80; //绘制单个元素 reorderableList.drawElementCallback = (rect, index, isActive, isFocused) => { var element = prop.GetArrayElementAtIndex(index); rect.height -= 4; rect.y += 2; EditorGUI.PropertyField(rect, element); }; //背景色 reorderableList.drawElementBackgroundCallback = (rect, index, isActive, isFocused) => { GUI.backgroundColor = Color.yellow; }; //头部 reorderableList.drawHeaderCallback = (rect) => EditorGUI.LabelField(rect, prop.displayName); } public override void OnInspectorGUI() { serializedObject.Update(); reorderableList.DoLayoutList(); serializedObject.ApplyModifiedProperties(); }}
目前的效果如下图:
最后还需要利用PropertyDrawer来绘制单个Serializable类的每个实例的GUI,也就是Character类
using UnityEngine;using System.Collections;using UnityEditor;//定制Serializable类的每个实例的GUI[CustomPropertyDrawer(typeof(Character))]public class CharacterDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { //创建一个属性包装器,用于将常规GUI控件与SerializedProperty一起使用 using (new EditorGUI.PropertyScope(position, label, property)) { //设置属性名宽度 Name HP EditorGUIUtility.labelWidth = 60; //输入框高度,默认一行的高度 position.height = EditorGUIUtility.singleLineHeight; //ico 位置矩形 Rect iconRect = new Rect(position) { width = 68, height = 68 }; Rect nameRect = new Rect(position) { width = position.width - 70, //减去icon的width 64 x = position.x + 70 //在icon的基础上右移64 }; Rect hpRect = new Rect(nameRect) { //在name的基础上,y坐标下移 y = nameRect.y + EditorGUIUtility.singleLineHeight + 2 }; Rect powerRect = new Rect(hpRect) { //在hp的基础上,y坐标下移 y = hpRect.y + EditorGUIUtility.singleLineHeight + 2 }; Rect weaponLabelRect = new Rect(powerRect) { y = powerRect.y + EditorGUIUtility.singleLineHeight + 2, width = 60 }; Rect weaponRect = new Rect(weaponLabelRect) { x = weaponLabelRect.x + 60, width = powerRect.width - 60 }; //找到每个属性的序列化值 SerializedProperty iconProperty = property.FindPropertyRelative("icon"); SerializedProperty nameProperty = property.FindPropertyRelative("name"); SerializedProperty hpProperty = property.FindPropertyRelative("hp"); SerializedProperty powerProperty = property.FindPropertyRelative("power"); SerializedProperty weaponProperty = property.FindPropertyRelative("weapon"); //绘制icon iconProperty.objectReferenceValue = EditorGUI.ObjectField(iconRect, iconProperty.objectReferenceValue, typeof(Texture), false); //绘制name nameProperty.stringValue = EditorGUI.TextField(nameRect, nameProperty.displayName, nameProperty.stringValue); //Slider,范围在0-100 EditorGUI.IntSlider(hpRect, hpProperty, 0, 100); //Slider,范围在0-10 EditorGUI.IntSlider(powerRect, powerProperty, 0, 10); EditorGUI.PrefixLabel(weaponLabelRect, new GUIContent("weapon")); EditorGUI.PropertyField(weaponRect, weaponProperty, GUIContent.none); } }}
最后效果就是文章一开始的gif图了
示例工程链接:
链接:http://pan.baidu.com/s/1cgyZ98 密码:jbzh
以上知识分享,如有错误,欢迎指出,共同学习,共同进步
阅读全文
0 0
- Unity编辑器拓展之二:ReorderableList可重新排序的列表框(复杂使用)
- Unity编辑器拓展之一:ReorderableList可重新排序的列表框(简单使用)
- Unity ReorderableList 可重新排序的列表框使用
- 【unity拓展】使列表支持拖动排序(Reorderablelist)
- Unity编辑器拓展<二>
- Unity编辑器拓展之三:拓展Unity的Hierarchy面板
- Unity Editor 编辑器扩展 十一 Inspector可排序列表
- 【小松教你手游开发】【unity实用技能】拓展函数(给系统代码添加可直接使用的接口)
- unity编辑器拓展
- Unity编辑器拓展<一>
- unity拓展编辑器
- 【基础算法】排序-复杂排序之二(快速排序)
- unity编辑器拓展一——删除项目工程里的未使用Shader
- 那些复杂的列表总要弄个清楚——(四)激动人心的BaseAdapter之二
- Unity编辑器拓展之四:ConsoleWindow中的双击日志定位
- 【基础算法】排序-复杂排序之二(找出第K大的数)
- 【unity编辑器拓展】使用脚本添加Prefab到场景中
- unity编辑器(二)----MenuItem的用法
- 职场沟通中还分上下左右?
- C语言编程初识json
- vyos
- 【南阳理工】 56 阶乘因式分解(一)
- const 和 #define 的区别
- Unity编辑器拓展之二:ReorderableList可重新排序的列表框(复杂使用)
- R语言自定义函数及基本分支循环结构
- spark2.2.0源码学习过程记录:Day4
- hdu_2647_拓扑排序_邻接表或者vector_优化内存_拓扑+贪心_用的是拓扑思想_并不是拓扑
- 主席树详解
- 小程序数字精确度问题
- spark2.2.0源码学习过程记录:Day5
- PsSetCreateProcessNotifyRoutine妙用
- 利用Data URL(data:image/jpg;base64,)将小图片生成数据流形式