TH1/Unity/Assets/Editor/TechInfoDrawer.cs
2025-11-19 21:11:18 +08:00

152 lines
5.1 KiB
C#

using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(TechInfo))]
public class TechInfoDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
bool isTechAtomList = property.propertyPath.Contains("TechAtomList");
// Step 1: 获取 TechId
var techIdProp = property.FindPropertyRelative("TechType");
var techNameProp =property.FindPropertyRelative("TechName");
string techIdText = techIdProp != null ? techIdProp.uintValue.ToString() : "???";
string techNameText = techNameProp != null ? techNameProp.stringValue : "Unnamed";
string rawLabel = $"#{techIdText} {techNameText}";
// 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;
}
}
[CustomPropertyDrawer(typeof(TechAtomInfo))]
public class TechAtomInfoDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Step 1: 获取 TechId
var techAtomIdProp = property.FindPropertyRelative("TechAtom");
var techAtomNameProp =property.FindPropertyRelative("TechAtomName");
string techAtomIdText = techAtomIdProp != null ? techAtomIdProp.uintValue.ToString() : "???";
string techAtomNameText = techAtomNameProp != null ? techAtomNameProp.stringValue : "Unnamed";
string rawAtomLabel = $"#{techAtomIdText} {techAtomNameText}";
// Step 4: 显示 Foldout
property.isExpanded = EditorGUI.Foldout(
new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight),
property.isExpanded,
rawAtomLabel
);
// 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;
}
}