自定义类
[System.Serializable] public class Test { public string name; public int age; }
数据类
using System.Collections.Generic; using UnityEngine; [CreateAssetMenu] public class TestSO : ScriptableObject { public List<Test> testList; }
显示如下

修改数据类显示
数据元素内容显示为一行
注意:不能使用自动布局api
using UnityEngine; using UnityEditor; [CustomPropertyDrawer(typeof(Test))] public class TestDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { var spName = property.FindPropertyRelative("name"); var spAge = property.FindPropertyRelative("age"); var rect1 = position; var rect2 = position; var indentlevel = EditorGUI.indentLevel; var lableWidth = EditorGUIUtility.labelWidth; EditorGUI.indentLevel = 2; EditorGUIUtility.labelWidth = 60; rect1.width = position.width / 2; rect1.height = EditorGUIUtility.singleLineHeight; rect2.width = position.width / 2; rect2.height = EditorGUIUtility.singleLineHeight; rect2.x = position.width / 2+40; rect2.y = rect1.y; EditorGUI.PropertyField(rect1, spName, new GUIContent("名字")); EditorGUI.PropertyField(rect2, spAge, new GUIContent("年龄")); EditorGUI.indentLevel = indentlevel; EditorGUIUtility.labelWidth = lableWidth; //EditorGUI.DrawRect(rect1, Color.green); //EditorGUI.DrawRect(rect2, Color.gray); } //获取属性高度 public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { return base.GetPropertyHeight(property, label); } }
修改后显示效果如下

TestList修改为指定名称;Element修改为指定名称
using UnityEngine; using UnityEditor; using UnityEditorInternal; [CustomEditor(typeof(TestSO))] public class TestSOEditor : Editor { SerializedProperty sPTestList; ReorderableList arrayList; private void OnEnable() { sPTestList = serializedObject.FindProperty("testList"); if (arrayList == null) { arrayList = new ReorderableList(serializedObject, sPTestList, true, true, true, true); //绘制Header arrayList.drawHeaderCallback += DrawHeader; //绘制数组元素 arrayList.drawElementCallback += DrawElement; //获取数组的高度 arrayList.elementHeightCallback += DrawElementHeight; } } void DrawHeader(Rect rect) { EditorGUI.LabelField(rect, "测试列表"); } void DrawElement(Rect rect, int index, bool isActive, bool isFocused) { var element = sPTestList.GetArrayElementAtIndex(index); var arrowRect = rect; int indentLevel = EditorGUI.indentLevel; EditorGUI.indentLevel = 1; arrowRect.height = EditorGUIUtility.singleLineHeight; element.isExpanded = EditorGUI.Foldout(arrowRect, element.isExpanded, new GUIContent("元素" + index)); EditorGUI.indentLevel = indentLevel; //EditorGUI.DrawRect(rect, Color.red); //EditorGUI.DrawRect(arrowRect, Color.blue); if (element.isExpanded) { rect.y += arrowRect.height; EditorGUI.PropertyField(rect, element); } } float DrawElementHeight(int index) { var element = sPTestList.GetArrayElementAtIndex(index); var height = EditorGUIUtility.singleLineHeight;//折叠箭头的高度 if (element.isExpanded)//如果元素展开 获取展开内容的高度和箭头的高度之和 height += EditorGUI.GetPropertyHeight(element); return height; } public override void OnInspectorGUI() { serializedObject.Update(); arrayList.DoLayoutList(); serializedObject.ApplyModifiedProperties(); } }
修改后如下图所示

以上内容转自https://blog.csdn.net/weixin_43796392/article/details/143320943
浙公网安备 33010602011771号