TH1/Unity/Assets/Editor/HeroTaskInfoDrawer.cs
2025-10-24 20:22:08 +08:00

74 lines
2.4 KiB
C#

using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(HeroInfoData))]
public class HeroTaskInfoDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Step 1: 获取 hintId
var heroTypeProp = property.FindPropertyRelative("GiantType");
string heroTaskText = heroTypeProp != null ? heroTypeProp.enumNames[heroTypeProp.enumValueIndex] : "Unnamed";
string rawLabel = $"{heroTaskText}";
// Step 4: 显示 Foldout
property.isExpanded = EditorGUI.Foldout(
new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight),
property.isExpanded,
rawLabel
);
// 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;
}
}