using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(SkillInfo))] public class SkillInfoDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Step 1: 获取 SkillType var skillProp = property.FindPropertyRelative("SkillType"); string skillName = skillProp?.enumDisplayNames[skillProp.enumValueIndex]; // Step 3: 构造标题 //label.text = unitTypeName; label.text = skillName; // Step 4: 显示 Foldout property.isExpanded = EditorGUI.Foldout( new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), property.isExpanded, label ); // Step 5: 展开显示所有字段 if (property.isExpanded) { EditorGUI.indentLevel++; float y = position.y + EditorGUIUtility.singleLineHeight; SerializedProperty childProp = property.Copy(); SerializedProperty end = childProp.GetEndProperty(); childProp.NextVisible(true); // 进入第一个子属性 while (!SerializedProperty.EqualContents(childProp, end)) { float h = EditorGUI.GetPropertyHeight(childProp, true); EditorGUI.PropertyField( new Rect(position.x, y, position.width, h), childProp, true ); y += h + EditorGUIUtility.standardVerticalSpacing; if (!childProp.NextVisible(false)) break; } EditorGUI.indentLevel--; } } public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { float height = EditorGUIUtility.singleLineHeight; if (property.isExpanded) { SerializedProperty childProp = property.Copy(); SerializedProperty end = childProp.GetEndProperty(); childProp.NextVisible(true); // 进入第一个子属性 while (!SerializedProperty.EqualContents(childProp, end)) { height += EditorGUI.GetPropertyHeight(childProp, true) + EditorGUIUtility.standardVerticalSpacing; if (!childProp.NextVisible(false)) break; } } return height; } }