TH1/Unity/Assets/Scripts/TH1_Logic/Editor/GridVFXInfoPropertyDrawer.cs

92 lines
3.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEditor;
using UnityEngine;
namespace Logic.Editor
{
/// <summary>
/// GridVFXInfo 的自定义 PropertyDrawer在列表中显示 Type 名称
/// </summary>
[CustomPropertyDrawer(typeof(GridVFXInfoDataAssets.GridVFXInfo))]
public class GridVFXInfoPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
// 获取 Type 字段
var typeProperty = property.FindPropertyRelative("Type");
var displayNameProperty = property.FindPropertyRelative("DisplayName");
if (typeProperty != null)
{
// 构建显示文本Type 名称 (DisplayName)
string typeName = typeProperty.enumNames[typeProperty.enumValueIndex];
string displayText = displayNameProperty != null && !string.IsNullOrEmpty(displayNameProperty.stringValue)
? $"{typeName} ({displayNameProperty.stringValue})"
: typeName;
// 绘制折叠标题
property.isExpanded = EditorGUI.Foldout(
new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight),
property.isExpanded,
displayText,
true
);
// 如果展开,绘制所有字段
if (property.isExpanded)
{
EditorGUI.indentLevel++;
float lineHeight = EditorGUIUtility.singleLineHeight + 2;
float currentY = position.y + lineHeight;
// 遍历所有子属性并绘制
SerializedProperty iterator = property.Copy();
SerializedProperty endProperty = property.GetEndProperty();
bool enterChildren = true;
while (iterator.NextVisible(enterChildren) && !SerializedProperty.EqualContents(iterator, endProperty))
{
enterChildren = false;
float height = EditorGUI.GetPropertyHeight(iterator, true);
Rect lineRect = new Rect(position.x, currentY, position.width, height);
EditorGUI.PropertyField(lineRect, iterator, true);
currentY += height + 2;
}
EditorGUI.indentLevel--;
}
}
else
{
// 如果找不到 Type 字段,使用默认绘制
EditorGUI.PropertyField(position, property, label, true);
}
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (!property.isExpanded)
{
return EditorGUIUtility.singleLineHeight;
}
// 计算展开后的总高度
float height = EditorGUIUtility.singleLineHeight + 2;
SerializedProperty iterator = property.Copy();
SerializedProperty endProperty = property.GetEndProperty();
bool enterChildren = true;
while (iterator.NextVisible(enterChildren) && !SerializedProperty.EqualContents(iterator, endProperty))
{
enterChildren = false;
height += EditorGUI.GetPropertyHeight(iterator, true) + 2;
}
return height;
}
}
}