using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(ConditionalFieldAttribute))] public class ConditionalFieldDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { ConditionalFieldAttribute conditional = (ConditionalFieldAttribute)attribute; SerializedProperty conditionProperty = property.serializedObject.FindProperty(conditional.conditionField); if (conditionProperty != null && conditionProperty.boolValue) { EditorGUI.PropertyField(position, property, label, true); } } public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { ConditionalFieldAttribute conditional = (ConditionalFieldAttribute)attribute; SerializedProperty conditionProperty = property.serializedObject.FindProperty(conditional.conditionField); if (conditionProperty != null && conditionProperty.boolValue) { return EditorGUI.GetPropertyHeight(property, label, true); } return 0f; // 隐藏字段时的高度 } }