/// <summary> /// Updates the current attribute and compared property references. /// </summary> /// <param name="property">The SerializedProperty to make the custom GUI for.</param> /// <remarks> /// TODO: Move this code somewhere else as it applies to many attributes. /// </remarks> private void UpdateDrawerReferences(SerializedProperty property) { _disableIf = (DisableIfAttribute)attribute; string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, _disableIf.ComparedPropertyName) : _disableIf.ComparedPropertyName; _comparedProperty = property.serializedObject.FindProperty(path); }
public override void DrawProperty(SerializedProperty property) { bool drawDisabled = false; bool validCondition = false; DisableIfAttribute disableIfAttribute = PropertyUtility.GetAttribute <DisableIfAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); FieldInfo conditionField = ReflectionUtility.GetField(target, disableIfAttribute.ConditionName); if (conditionField != null && conditionField.FieldType == typeof(bool)) { drawDisabled = (bool)conditionField.GetValue(target); validCondition = true; } MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, disableIfAttribute.ConditionName); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { drawDisabled = (bool)conditionMethod.Invoke(target, null); validCondition = true; } if (validCondition) { GUI.enabled = !drawDisabled; EditorDrawUtility.DrawPropertyField(property); GUI.enabled = true; } else { string warning = disableIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, target); } }