TH1/Unity/Assets/Editor/ActionInfoDrawer.cs
2025-07-17 18:26:28 +08:00

106 lines
3.6 KiB
C#

using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(ActionInfo))]
public class ActionInfoDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var actionIdProp = property.FindPropertyRelative("ActionId");
var actionTypeProp = actionIdProp?.FindPropertyRelative("ActionType");
string actionTypeName = (actionTypeProp != null && actionTypeProp.propertyType == SerializedPropertyType.Enum)
? actionTypeProp.enumDisplayNames[actionTypeProp.enumValueIndex]
: "Unknown";
string secondaryName = "None";
if (actionIdProp != null)
{
SerializedProperty subProp = actionIdProp.Copy();
SerializedProperty endProp = subProp.GetEndProperty();
bool enterChildren = true;
if (subProp.NextVisible(enterChildren))
{
do
{
if (SerializedProperty.EqualContents(subProp, endProp))
break;
if (subProp.propertyType == SerializedPropertyType.Enum &&
subProp.name != "ActionType")
{
if (subProp.enumValueIndex != 0 && subProp.enumValueIndex < subProp.enumDisplayNames.Length)
{
secondaryName = subProp.enumDisplayNames[subProp.enumValueIndex];
break;
}
}
} while (subProp.NextVisible(false));
}
}
// 构造标题
string rawLabel = label.text;
string index = rawLabel.Replace("Element", "").Trim();
label.text = $"[{actionTypeName}: {secondaryName}] {index}";
// Foldout
property.isExpanded = EditorGUI.Foldout(
new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight),
property.isExpanded,
label
);
// 展开所有字段
if (property.isExpanded)
{
EditorGUI.indentLevel++;
float y = position.y + EditorGUIUtility.singleLineHeight;
SerializedProperty childProp = property.Copy();
SerializedProperty end = childProp.GetEndProperty();
if (childProp.NextVisible(true))
{
do
{
if (SerializedProperty.EqualContents(childProp, end))
break;
float h = EditorGUI.GetPropertyHeight(childProp, true);
EditorGUI.PropertyField(new Rect(position.x, y, position.width, h), childProp, true);
y += h + EditorGUIUtility.standardVerticalSpacing;
} while (childProp.NextVisible(false));
}
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();
if (childProp.NextVisible(true))
{
do
{
if (SerializedProperty.EqualContents(childProp, end))
break;
height += EditorGUI.GetPropertyHeight(childProp, true) + EditorGUIUtility.standardVerticalSpacing;
} while (childProp.NextVisible(false));
}
}
return height;
}
}