public override bool CanDrawProperty(SerializedProperty property) { HideIfAttribute hideIfAttribute = PropertyUtility.GetAttributes <HideIfAttribute>(property)[0]; UnityEngine.Object target = PropertyUtility.GetTargetObject(property); FieldInfo conditionField = target.GetType().GetField(hideIfAttribute.ConditionName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (conditionField != null && conditionField.FieldType == typeof(bool)) { return(!(bool)conditionField.GetValue(target)); } MethodInfo conditionMethod = target.GetType().GetMethod(hideIfAttribute.ConditionName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { return(!(bool)conditionMethod.Invoke(target, null)); } string warning = hideIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, target); return(true); }
public override bool CanDrawProperty(SerializedProperty property) { HideIfAttribute hideIfAttribute = PropertyUtility.GetAttribute <HideIfAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); FieldInfo conditionField = ReflectionUtility.GetField(target, hideIfAttribute.ConditionName); if (conditionField != null && conditionField.FieldType == typeof(bool)) { return(!(bool)conditionField.GetValue(target)); } MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, hideIfAttribute.ConditionName); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { return(!(bool)conditionMethod.Invoke(target, null)); } string warning = hideIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target); return(true); }
/// <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) { _hideIf = (HideIfAttribute)attribute; string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, _hideIf.ComparedPropertyName) : _hideIf.ComparedPropertyName; _comparedProperty = property.serializedObject.FindProperty(path); }
public static bool ShouldDraw(SerializedObject obj, HideIfAttribute attribute) { var prop = obj.FindProperty(attribute.variable); if (prop == null) { return(true); } return(prop.boolValue != attribute.state); }
private bool ShouldHide(HideIfAttribute hideAttr, SerializedProperty property) { if (hideAttr.paths.Length < 1) { return(false); } if (hideAttr.type != null) { return(ShouldHide0()); } else if (string.IsNullOrEmpty(hideAttr.relational)) { return(ShouldHide1()); } else { return(ShouldHide2()); } bool ShouldHide(string p, object v) { if (p == "typeof(this)") { return((v as System.Type).IsAssignableFrom(fieldInfo.ReflectedType)); } else if (ZetanUtility.Editor.TryGetValue(property, out _)) { bool type = p.StartsWith("typeof("); if (type) { p = p.Replace("typeof(", "").Replace(")", ""); } if (ZetanUtility.TryGetMemberValue(p, property.serializedObject.targetObject ?? property.managedReferenceValue, out var value, out _)) { if (type) { return(value.GetType().IsAssignableFrom(v as System.Type)); } else if (Equals(value, v)) { return(true); } else { return(false); } } else { Debug.LogWarning($"找不到路径:{(property.serializedObject.targetObject ?? property.managedReferenceValue).GetType().Name}.{p}"); return(false); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { HideIfAttribute hideAttr = (HideIfAttribute)attribute; bool hide = ShouldHide(hideAttr, property); if (!hide || hideAttr.readOnly) { label = EditorGUI.BeginProperty(position, label, property); EditorGUI.BeginDisabledGroup(hide && hideAttr.readOnly); EditorGUI.PropertyField(position, property, label, true); EditorGUI.EndDisabledGroup(); EditorGUI.EndProperty(); } }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { HideIfAttribute hideAttr = (HideIfAttribute)attribute; bool hide = ShouldHide(hideAttr, property); if (!hide || hideAttr.readOnly) { return(EditorGUI.GetPropertyHeight(property, label, true)); } else { return(-EditorGUIUtility.standardVerticalSpacing); } }
private void InitializeDrawer(SerializedProperty property) { ShowIfAttribute showIfAttr = (this.drawer.attribute as ShowIfAttribute); if (showIfAttr != null) { this.fieldName = showIfAttr.fieldName; this.@operator = showIfAttr.@operator; this.multiOperator = showIfAttr.multiOperator; this.values = showIfAttr.values; } else { HideIfAttribute hideIfAttr = (this.drawer.attribute as HideIfAttribute); if (hideIfAttr != null) { this.fieldName = hideIfAttr.fieldName; this.@operator = hideIfAttr.@operator; this.multiOperator = hideIfAttr.multiOperator; this.values = hideIfAttr.values; } else { this.errorAttribute = "ShowIfAttribute or HideIfAttribute is required by field " + this.name + "."; } } this.conditionField = this.drawer.fieldInfo.DeclaringType.GetField(this.fieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (this.conditionField == null) { this.errorAttribute = this.name + " is requiring field \"" + this.fieldName + "\"."; return; } else if (this.@operator != Op.None) { if (this.values[0] == null) { this.targetValueStringified = new string[] { string.Empty }; this.PropertyHeight = this.GetHeightAllOpsString; if (this.@operator != Op.Equals && this.@operator != Op.Diff) { this.errorAttribute = this.name + " is requiring a null value whereas its operator is \"" + this.@operator + "\" which is impossible."; } } else if (this.values[0] is Boolean) { this.targetValueStringified = new string[] { this.values[0].ToString() }; this.PropertyHeight = this.GetHeightAllOpsString; if (this.@operator != Op.Equals && this.@operator != Op.Diff) { this.errorAttribute = this.name + " is requiring a boolean whereas its operator is \"" + this.@operator + "\" which is impossible."; } } else if (this.values[0] is Int32 || this.values[0] is Single || this.values[0] is Enum || this.values[0] is Double || this.values[0] is Decimal || this.values[0] is Int16 || this.values[0] is Int64 || this.values[0] is UInt16 || this.values[0] is UInt32 || this.values[0] is UInt64 || this.values[0] is Byte || this.values[0] is SByte) { this.targetValueDecimaled = new Decimal[] { Convert.ToDecimal(this.values[0]) }; this.PropertyHeight = this.GetHeightAllOpsScalar; } else { this.targetValueStringified = new string[] { this.values[0].ToString() }; this.PropertyHeight = this.GetHeightAllOpsString; } } else if (this.multiOperator != MultiOp.None) { if (this.CheckUseOfNonScalarValue() == true) { this.targetValueStringified = new string[this.values.Length]; for (int i = 0; i < this.values.Length; i++) { if (this.values[i] != null) { this.targetValueStringified[i] = this.values[i].ToString(); } else { this.targetValueStringified[i] = string.Empty; } } this.PropertyHeight = this.GetHeightMultiOpsString; } else { this.targetValueDecimaled = new Decimal[this.values.Length]; for (int i = 0; i < this.values.Length; i++) { this.targetValueDecimaled[i] = Convert.ToDecimal(this.values[i]); } this.PropertyHeight = this.GetHeightMultiOpsScalar; } } // Force the next update. object newValue = this.conditionField.GetValue(property.serializedObject.targetObject); if (this.lastValue == newValue) { this.lastValue = true; } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Set the global variables. drawIf = attribute as HideIfAttribute; comparedField = property.serializedObject.FindProperty(drawIf.comparedPropertyName); // Get the value of the compared field. object comparedFieldValue = comparedField.GetValue <object>(); // References to the values as numeric types. NumericType numericComparedFieldValue = null; NumericType numericComparedValue = null; try { // Try to set the numeric types. numericComparedFieldValue = new NumericType(comparedFieldValue); numericComparedValue = new NumericType(drawIf.comparedValue); } catch (NumericTypeExpectedException) { // This place will only be reached if the type is not a numeric one. If the comparison type is not valid for the compared field type, log an error. if (drawIf.comparisonType != ComparisonType.Equals && drawIf.comparisonType != ComparisonType.NotEqual) { Debug.LogError("The only comparsion types available to type '" + comparedFieldValue.GetType() + "' are Equals and NotEqual. (On object '" + property.serializedObject.targetObject.name + "')"); return; } } // Is the condition met? Should the field be drawn? bool conditionMet = false; // Compare the values to see if the condition is met. switch (drawIf.comparisonType) { case ComparisonType.Equals: if (!comparedFieldValue.Equals(drawIf.comparedValue)) { conditionMet = true; } break; case ComparisonType.NotEqual: if (!comparedFieldValue.Equals(drawIf.comparedValue)) { conditionMet = true; } break; case ComparisonType.GreaterThan: if (numericComparedFieldValue < numericComparedValue) { conditionMet = true; } break; case ComparisonType.SmallerThan: if (numericComparedFieldValue > numericComparedValue) { conditionMet = true; } break; case ComparisonType.SmallerOrEqual: if (numericComparedFieldValue >= numericComparedValue) { conditionMet = true; } break; case ComparisonType.GreaterOrEqual: if (numericComparedFieldValue <= numericComparedValue) { conditionMet = true; } break; } // The height of the property should be defaulted to the default height. propertyHeight = base.GetPropertyHeight(property, label); // If the condition is met, simply draw the field. Else... if (conditionMet) { EditorGUI.PropertyField(position, property); } else { //...check if the disabling type is read only. If it is, draw it disabled, else, set the height to zero. if (drawIf.disablingType == HidingType.ReadOnly) { GUI.enabled = false; EditorGUI.PropertyField(position, property); GUI.enabled = true; } else { propertyHeight = 0f; } } }