private bool GetConditionalAttributeResult(ShowIfAttribute condHAtt, SerializedProperty propertyA) { bool enabled = true; SerializedProperty sourcePropertyValue = FindSerializableProperty(condHAtt, propertyA); if (sourcePropertyValue != null) { var fieldValue = GetPropertyValue(sourcePropertyValue); var comparingValue = condHAtt.CompareValue.ToString(); var fieldValueString = fieldValue.ToString(); enabled = comparingValue == fieldValueString; } else { Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.SourceField); } if (condHAtt.Inverse) { enabled = !enabled; } return(enabled); }
bool IsVisible(Object TargetObject, ShowIfAttribute Attrib) { var TargetObjectType = TargetObject.GetType(); if (CachedEventMethodInfo == null) { CachedEventMethodInfo = TargetObjectType.GetMethod(Attrib.FunctionName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); } if (CachedEventMethodInfo != null) { var Result = CachedEventMethodInfo.Invoke(TargetObject, null); var ResultType = (Result == null) ? "null" : Result.GetType().Name; try { var ResultBool = (bool)Result; return(ResultBool); } catch (System.Exception e) { Debug.LogWarning("Failed to get event " + Attrib.FunctionName + " in " + TargetObjectType + " result as bool (is " + ResultType + "); " + e.Message); } } Debug.LogWarning("ShowIfAttribute: Unable to find method " + Attrib.FunctionName + " in " + TargetObjectType); return(false); }
public override bool CanDrawProperty(SerializedProperty property) { ShowIfAttribute showIfAttribute = PropertyUtility.GetAttribute <ShowIfAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); FieldInfo conditionField = ReflectionUtility.GetField(target, showIfAttribute.ConditionName); if (conditionField != null && conditionField.FieldType == typeof(bool)) { return((bool)conditionField.GetValue(target)); } MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, showIfAttribute.ConditionName); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { return((bool)conditionMethod.Invoke(target, null)); } string warning = showIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, logToConsole: true, context: target); return(true); }
private static bool CheckShowTargets(SerializedProperty property, ShowIfAttribute orAttribute) { bool res = true; if (orAttribute.mode == ShowIfAttribute.Mode.And) { for (int i = 0; i < orAttribute.targets.Length; i++) { res &= CheckShowTarget(property, orAttribute.targets[i]); if (!res) { return(false); } } return(res); } res = false; for (int i = 0; i < orAttribute.targets.Length; i++) { res |= CheckShowTarget(property, orAttribute.targets[i]); if (res) { return(true); } } return(res); }
private bool CanShowProperty(SerializedProperty property) { ShowIfAttribute showIf = attribute as ShowIfAttribute; //if ((attribute as ProgressBarAttribute).hideWhenZero && property.floatValue <= 0) // return; var enumField = property.serializedObject.FindProperty(showIf.EnumField); var enumValue = showIf.EnumValue; //bool ok = enumField.enumValueIndex & enumValue; //object enumFieldValue = enumField.objectReferenceValue; int enumFieldInt = enumField.intValue; int enumValueInt = (int)enumValue; // Only draw if it matches bool ok = (enumFieldInt & enumValueInt) != 0; /* * Debug.Log(""); * Debug.Log("Field : [" + enumField.name + "]"); * Debug.Log("Value : [" + enumValue + "]"); * Debug.Log("Field a: [" + enumField.intValue + "]"); * Debug.Log("Value a: [" + (int) enumValue + "]"); * Debug.Log("Ok? : [" + ok + "]"); */ return(ok); }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { ShowIfAttribute attribute = (ShowIfAttribute)this.attribute; isShow = CheckShowTargets(property, attribute); return(isShow ? base.GetPropertyHeight(property, label) : -2); }
public override bool CanDrawProperty(SerializedProperty property) { ShowIfAttribute showIfAttribute = PropertyUtility.GetAttributes <ShowIfAttribute>(property)[0]; UnityEngine.Object target = PropertyUtility.GetTargetObject(property); FieldInfo conditionField = target.GetType().GetField(showIfAttribute.ConditionName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (conditionField != null && conditionField.FieldType == typeof(bool)) { return((bool)conditionField.GetValue(target)); } MethodInfo conditionMethod = target.GetType().GetMethod(showIfAttribute.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 = showIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, 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) { _showIf = (ShowIfAttribute)attribute; string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, _showIf.ComparedPropertyName) : _showIf.ComparedPropertyName; _comparedProperty = property.serializedObject.FindProperty(path); }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { ShowIfAttribute attribute = (ShowIfAttribute)this.attribute; isShow = CheckShowTargets(property, attribute); float height = isShow ? EditorGUI.GetPropertyHeight(property, label, property.hasVisibleChildren) : -2; return(height); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { ShowIfAttribute showIfAttrib = (ShowIfAttribute)attribute; if (showIfAttrib.condition) { EditorGUI.PropertyField(position, property, label, true); } }
bool IsShowing(SerializedProperty property) { ShowIfAttribute si = attribute as ShowIfAttribute; SerializedProperty prop = property.serializedObject.FindProperty(si.varName); bool show = true; if (prop != null) { if (prop.propertyType == SerializedPropertyType.Boolean) { switch (si.comparison) { case ShowIfAttribute.Comparison.Equals: show = prop.boolValue; break; case ShowIfAttribute.Comparison.Not: show = !prop.boolValue; break; } } if (prop.propertyType == SerializedPropertyType.Integer || prop.propertyType == SerializedPropertyType.Enum) { switch (si.comparison) { case ShowIfAttribute.Comparison.Equals: show = prop.intValue == si.threshold; break; case ShowIfAttribute.Comparison.Not: show = prop.intValue != si.threshold; break; case ShowIfAttribute.Comparison.Greater: show = prop.intValue > si.threshold; break; case ShowIfAttribute.Comparison.Less: show = prop.intValue < si.threshold; break; } } if (prop.propertyType == SerializedPropertyType.Float) { switch (si.comparison) { case ShowIfAttribute.Comparison.Equals: show = prop.floatValue == si.threshold; break; case ShowIfAttribute.Comparison.Not: show = prop.floatValue != si.threshold; break; case ShowIfAttribute.Comparison.Greater: show = prop.floatValue > si.threshold; break; case ShowIfAttribute.Comparison.Less: show = prop.floatValue < si.threshold; break; } } if (prop.propertyType == SerializedPropertyType.ObjectReference) { switch (si.comparison) { case ShowIfAttribute.Comparison.Not: show = prop.objectReferenceValue == null; break; default: show = prop.objectReferenceValue != null; break; } } } return(show); }
private bool ShouldShow(ShowIfAttribute hideAttr, SerializedProperty property) { if (hideAttr.paths.Length < 1) { return(false); } if (hideAttr.type != null) { return(ShouldShow0()); } else if (string.IsNullOrEmpty(hideAttr.relational)) { return(ShouldShow1()); } else { return(ShouldShow2()); } bool ShouldShow(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 float GetPropertyHeight(SerializedProperty property, GUIContent label) { ShowIfAttribute showIfAttrib = (ShowIfAttribute)attribute; if (showIfAttrib.condition) { return(EditorGUI.GetPropertyHeight(property, label)); } else { return(0); } }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { ShowIfAttribute condHAtt = (ShowIfAttribute)attribute; bool enabled = GetConditionalAttributeResult(condHAtt, property); if (!condHAtt.HideInInspector || enabled) { return(EditorGUI.GetPropertyHeight(property, label)); } else { return(-EditorGUIUtility.standardVerticalSpacing); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { ShowIfAttribute hideAttr = (ShowIfAttribute)attribute; bool show = ShouldShow(hideAttr, property); if (show || hideAttr.readOnly) { label = EditorGUI.BeginProperty(position, label, property); EditorGUI.BeginDisabledGroup(show && hideAttr.readOnly); EditorGUI.PropertyField(position, property, label, true); EditorGUI.EndDisabledGroup(); EditorGUI.EndProperty(); } }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { ShowIfAttribute showAttr = (ShowIfAttribute)attribute; bool show = ShouldShow(showAttr, property); if (show || showAttr.readOnly) { return(EditorGUI.GetPropertyHeight(property, label, true)); } else { return(-EditorGUIUtility.standardVerticalSpacing); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (showIfAttribute == null || checkProperty == null) { showIfAttribute = (ShowIfAttribute)attribute; checkProperty = property.serializedObject.FindProperty(showIfAttribute.propertyName); } bool show = PropertyShown(checkProperty, showIfAttribute.propertyValue); if (show) { EditorGUI.PropertyField(position, property); } }
private SerializedProperty FindSerializableProperty(ShowIfAttribute condHAtt, SerializedProperty property) { string propertyPath = property.propertyPath; int idx = propertyPath.LastIndexOf('.'); if (idx == -1) { return(property.serializedObject.FindProperty(condHAtt.SourceField)); } else { propertyPath = propertyPath.Substring(0, idx); return(property.serializedObject.FindProperty(propertyPath).FindPropertyRelative(condHAtt.SourceField)); } }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { if (showIfAttribute == null || checkProperty == null) { showIfAttribute = (ShowIfAttribute)attribute; checkProperty = property.serializedObject.FindProperty(showIfAttribute.propertyName); } if (PropertyShown(checkProperty, showIfAttribute.propertyValue)) { return(base.GetPropertyHeight(property, label)); } return(0); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { ShowIfAttribute condHAtt = (ShowIfAttribute)attribute; bool enabled = GetConditionalAttributeResult(condHAtt, property); bool wasEnabled = GUI.enabled; GUI.enabled = enabled; if (!condHAtt.HideInInspector || enabled) { EditorGUI.PropertyField(position, property, label, true); } GUI.enabled = wasEnabled; }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { ShowIfAttribute showIfAtt = (ShowIfAttribute)attribute; bool enabled = GetShowIfAttributeResult(showIfAtt, property); if (!showIfAtt.boolValueNeeded || enabled) { return(EditorGUI.GetPropertyHeight(property, label)); } else { //The property is not being drawn //We want to undo the spacing added before and after the property return(-EditorGUIUtility.standardVerticalSpacing); } }
private bool GetShowIfAttributeResult(ShowIfAttribute showIfAtt, SerializedProperty property) { bool enabled = true; //Look for the sourcefield within the object that the property belongs to string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, showIfAtt.ConditionalSourceField); //changes the path to the conditionalsource property path SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(conditionPath); if (sourcePropertyValue != null) { //SHOW IF SOURCEPROPERTY IS CORRECT BOOL VALUE if (sourcePropertyValue != null && sourcePropertyValue.propertyType == SerializedPropertyType.Boolean) { enabled = sourcePropertyValue.boolValue; } //SHOW IF SOURCEPROPERTY IS CORRECT INT VALUE (USED ALSO FOR ENUMERATORS) else if (sourcePropertyValue != null && (sourcePropertyValue.propertyType == SerializedPropertyType.Integer || sourcePropertyValue.propertyType == SerializedPropertyType.Enum)) { enabled = showIfAtt.intValuesNeeded.Contains(sourcePropertyValue.intValue); } /*else if (sourcePropertyValue != null && sourcePropertyValue.propertyType == SerializedPropertyType.Enum) * { * enabled = sourcePropertyValue.intValue == showIfAtt.intValueNeeded; * }*/ else { Debug.LogWarning("Property type is not managed Yet"); } } else { Debug.LogWarning("WARNING: " + showIfAtt.ConditionalSourceField + " variable does not exist"); } return(enabled); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { //get the attribute data ShowIfAttribute showIfAtt = (ShowIfAttribute)attribute; //check if the propery we want to draw should be enabled bool enabled = GetShowIfAttributeResult(showIfAtt, property); //Enable/disable the property bool wasEnabled = GUI.enabled; GUI.enabled = enabled; //Check if we should draw the property if ( (property.propertyType == SerializedPropertyType.Boolean && !showIfAtt.boolValueNeeded || enabled) ) { EditorGUI.PropertyField(position, property, label, true); } //Ensure that the next property that is being drawn uses the correct settings GUI.enabled = wasEnabled; }
public static bool Eval(object obj, ShowIfAttribute attribute) { bool isTrue = false; var member = obj.GetType().GetMember(attribute.propertyName, bindingFlags).FirstOrDefault(); if (member == null) { isTrue = true; } else if (member.GetValue(obj) is object value) { if (attribute.useValue) { isTrue = Equals(value, attribute.value); } else if (value is bool) { isTrue = (bool)value == true; } else if (value is Object) { isTrue = (Object)value; } else if (value != null) { isTrue = true; } } if (attribute.invert) { isTrue = !isTrue; } return(isTrue); }
public override bool CanDrawProperty(SerializedProperty property) { ShowIfAttribute showIfAttribute = PropertyUtility.GetAttribute <ShowIfAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); List <bool> conditionValues = new List <bool>(); foreach (var condition in showIfAttribute.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 draw; if (showIfAttribute.ConditionOperator == ConditionOperator.And) { draw = true; foreach (var value in conditionValues) { draw = draw && value; } } else { draw = false; foreach (var value in conditionValues) { draw = draw || value; } } if (showIfAttribute.Reversed) { draw = !draw; } return(draw); } else { string warning = showIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target); return(true); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Set the global variables. hideShow = attribute as ShowIfAttribute; comparedField = property.serializedObject.FindProperty(hideShow.ComparedPropertyName); object comparedFieldValue = null; // Get the value of the compared field. switch (comparedField.propertyType) { case SerializedPropertyType.Boolean: comparedFieldValue = comparedField.boolValue; break; case SerializedPropertyType.String: comparedFieldValue = comparedField.stringValue; break; case SerializedPropertyType.Float: comparedFieldValue = comparedField.floatValue; break; case SerializedPropertyType.Integer: comparedFieldValue = comparedField.intValue; break; case SerializedPropertyType.Enum: comparedFieldValue = comparedField.enumNames[comparedField.enumValueIndex]; break; case SerializedPropertyType.ObjectReference: comparedFieldValue = comparedField.objectReferenceValue; break; } if (comparedFieldValue == null) { return; } // Is the condition met? Should the field be drawn? bool conditionMet = false; // Compare the values to see if the condition is met. switch (hideShow.ComparisonType) { case ShowIfComparisonType.Equals: if (comparedFieldValue.Equals(hideShow.ComparedValue)) { conditionMet = true; } break; case ShowIfComparisonType.NotEqual: if (!comparedFieldValue.Equals(hideShow.ComparedValue)) { 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 { propertyHeight = 0f; } }
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; } }