public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { showIfAttribute = attribute as ShowIfAttribute; EditorGUI.BeginProperty(position, label, property); if (Show(property)) { if (showIfAttribute.indent) { EditorGUI.indentLevel++; } Draw(position, property, attribute, label); if (showIfAttribute.indent) { EditorGUI.indentLevel--; } } else if (showIfAttribute.mode == ShowIfMode.Disabled) { if (showIfAttribute.indent) { EditorGUI.indentLevel++; } GUI.enabled = false; Draw(position, property, attribute, label); GUI.enabled = true; if (showIfAttribute.indent) { EditorGUI.indentLevel--; } } EditorGUI.EndProperty(); }
protected bool Show(SerializedProperty property) { showIfAttribute = attribute as ShowIfAttribute; var path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, showIfAttribute.propName) : showIfAttribute.propName; prop = property.serializedObject.FindProperty(path); if (prop == null) { return(true); } switch (prop.propertyType) { case SerializedPropertyType.Enum: return(prop.enumValueIndex.Equals((int)showIfAttribute.propValue)); case SerializedPropertyType.Boolean: return(prop.boolValue.Equals(showIfAttribute.propValue)); case SerializedPropertyType.Float: return(prop.floatValue > (float)showIfAttribute.propValue && prop.floatValue < (float)showIfAttribute.otherPropValue); case SerializedPropertyType.LayerMask: return(prop.intValue != 0); case SerializedPropertyType.String: return(prop.stringValue != string.Empty && prop.stringValue != ""); case SerializedPropertyType.Vector2: float sqrMag2 = prop.vector2Value.sqrMagnitude; return(sqrMag2 > (float)showIfAttribute.propValue && sqrMag2 < (float)showIfAttribute.otherPropValue); case SerializedPropertyType.Vector3: float sqrMag3 = prop.vector3Value.sqrMagnitude; return(sqrMag3 > (float)showIfAttribute.propValue && sqrMag3 < (float)showIfAttribute.otherPropValue); case SerializedPropertyType.Vector4: float sqrMag4 = prop.vector4Value.sqrMagnitude; return(sqrMag4 > (float)showIfAttribute.propValue && sqrMag4 < (float)showIfAttribute.otherPropValue); case SerializedPropertyType.ObjectReference: return(prop.objectReferenceValue != null); default: Debug.LogError("Unsupported ShowIf property type: " + prop.propertyType); return(true); } }