TH1/My project/Assets/Editor/HintInfoDrawer.cs
2025-07-04 03:19:24 +08:00

76 lines
2.6 KiB
C#

using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(HintInfo))]
public class HintInfoDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Step 1: 获取 hintId
var hintIdProp = property.FindPropertyRelative("HintType");
var hintNameProp =property.FindPropertyRelative("HintName");
string hintIdText = hintIdProp != null ? hintIdProp.uintValue.ToString() : "???";
string hintNameText = hintNameProp != null ? hintNameProp.stringValue : "Unnamed";
string rawLabel = $"{hintNameText}";
// 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;
}
}