/// <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) { _enableIf = (EnableIfAttribute)attribute; string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, _enableIf.ComparedPropertyName) : _enableIf.ComparedPropertyName; _comparedProperty = property.serializedObject.FindProperty(path); }
public override void DrawProperty(SerializedProperty property) { bool drawEnabled = false; bool validCondition = false; EnableIfAttribute enableIfAttribute = PropertyUtility.GetAttribute <EnableIfAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); FieldInfo conditionField = ReflectionUtility.GetField(target, enableIfAttribute.ConditionName); if (conditionField != null && conditionField.FieldType == typeof(bool)) { drawEnabled = (bool)conditionField.GetValue(target); validCondition = true; } MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, enableIfAttribute.ConditionName); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { drawEnabled = (bool)conditionMethod.Invoke(target, null); validCondition = true; } if (validCondition) { GUI.enabled = drawEnabled; EditorDrawUtility.DrawPropertyField(property); GUI.enabled = true; } else { string warning = enableIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, target); } }
public override void DrawProperty(SerializedProperty property) { EnableIfAttribute enableIfAttribute = PropertyUtility.GetAttribute <EnableIfAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); List <bool> conditionValues = new List <bool>(); foreach (var condition in enableIfAttribute.Conditions) { FieldInfo conditionField = ReflectionUtility.GetField(target, condition); if (conditionField != null && conditionField.FieldType == typeof(bool)) { conditionValues.Add((bool)conditionField.GetValue(target)); } MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, condition); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { conditionValues.Add((bool)conditionMethod.Invoke(target, null)); } } if (conditionValues.Count > 0) { bool enabled; if (enableIfAttribute.ConditionOperator == ConditionOperator.And) { enabled = true; foreach (var value in conditionValues) { enabled = enabled && value; } } else { enabled = false; foreach (var value in conditionValues) { enabled = enabled || value; } } if (enableIfAttribute.Reversed) { enabled = !enabled; } GUI.enabled = enabled; EditorDrawUtility.DrawPropertyField(property); GUI.enabled = true; } else { string warning = enableIfAttribute.GetType().Name + " 作用在一个有效的布尔值字段条件或着一个方法方法名!!"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target); } }