public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { ConditionalAttribute condHAtt = (ConditionalAttribute)this.attribute; bool showProperty = ShouldShowProperty(condHAtt, property); if (showProperty) { return(EditorGUI.GetPropertyHeight(property, label)); } else { return(-EditorGUIUtility.standardVerticalSpacing); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { Debug.Log("Show: "); ConditionalAttribute condHAtt = (ConditionalAttribute)this.attribute; bool showProperty = ShouldShowProperty(condHAtt, property); bool wasEnabled = GUI.enabled; GUI.enabled = showProperty; if (showProperty) { EditorGUI.PropertyField(position, property, label, true); } GUI.enabled = wasEnabled; }
private bool ShouldShowProperty(ConditionalAttribute attribute, SerializedProperty property) { bool isTrue = true; SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(attribute.ConditionalSourceField); if (sourcePropertyValue != null) { switch (sourcePropertyValue.propertyType) { case SerializedPropertyType.Boolean: isTrue = sourcePropertyValue.boolValue; break; case SerializedPropertyType.ObjectReference: isTrue = sourcePropertyValue.objectReferenceValue != null; break; case SerializedPropertyType.Enum: if (attribute.SpecificValue != null) { isTrue = false; foreach (object value in attribute.SpecificValue) { int enumValue = (int)Convert.ChangeType(value, TypeCode.Int32); isTrue = (sourcePropertyValue.enumValueIndex == enumValue); if (isTrue) { break; } } } break; } } else { object parent = GetParent(property); Type eventOwnerType = parent.GetType(); FieldInfo reflectedProperty = eventOwnerType.GetField(attribute.ConditionalSourceField); if (reflectedProperty != null) { object reflectedValue = reflectedProperty.GetValue(parent); if (reflectedValue is bool) { isTrue = (bool)reflectedValue; } else if (attribute.SpecificValue != null) { if (reflectedValue is Enum) { isTrue = false; int reflectedEnumValue = (int)Convert.ChangeType(reflectedValue, TypeCode.Int32); foreach (object value in attribute.SpecificValue) { int enumValue = (int)Convert.ChangeType(value, TypeCode.Int32); isTrue = (reflectedEnumValue == enumValue); if (isTrue) { break; } } } else { Debug.Log("SpecificValue must currently be an enum."); } } else { isTrue = reflectedValue != null; } } else { Debug.LogError( "Attempting to use a ConditionalAttribute, but \"" + attribute.ConditionalSourceField + "\" not found in " + GetParent(property).GetType() + ". Is the inspect object nested in an array?"); } } if (attribute.HideInInspectorIfTrue) { return(!isTrue); } return(isTrue); }