protected bool IsVisible(SerializedProperty property) { ShowWhenAttribute attrib = this.attribute as ShowWhenAttribute; if (attrib == null) { return(true); } if (attrib.type == ShowWhenAttribute.Type.None) { return(true); } SerializedProperty prop = property.serializedObject.FindProperty(attrib.propertyName); if (prop == null) { //Debug.LogError("ConditionalAttribute: unknown property name: " + attrib.propertyName + " (used in class '" + property.serializedObject.targetObject.GetType() + "')"); return(true); } Type targetObjectType = prop.serializedObject.targetObject.GetType(); FieldInfo[] fieldInfos = targetObjectType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); // firstly check if referenced properties are visible if (!IsReferencedPropertiesVisible(fieldInfos, property.serializedObject, attrib.propertyName)) { return(false); } return(IsPropertyVisible(prop, attrib)); }
private bool IsReferencedPropertiesVisible(FieldInfo[] fieldInfos, SerializedObject serializedObj, string fieldName) { foreach (var field in fieldInfos) { if (field.Name == fieldName) { object[] attribs = field.GetCustomAttributes(true); foreach (object attrib in attribs) { ShowWhenAttribute swa = attrib as ShowWhenAttribute; if (swa != null) { SerializedProperty prop = serializedObj.FindProperty(swa.propertyName); if (prop != null) { if (!IsPropertyVisible(prop, swa)) { return(false); } if (!IsReferencedPropertiesVisible(fieldInfos, serializedObj, prop.name)) { return(false); } break; } } } break; } } return(true); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { ShowWhenAttribute attribute = (ShowWhenAttribute)this.attribute; SerializedProperty conditionField = property.serializedObject.FindProperty(attribute.conditionFieldName); // We check that exist a Field with the parameter name if (conditionField == null) { ShowError(position, label, "Error getting the condition Field. Check the name."); return; } switch (conditionField.propertyType) { case SerializedPropertyType.Boolean: try { bool comparationValue = attribute.comparationValue == null || (bool)attribute.comparationValue; showField = conditionField.boolValue == comparationValue; } catch { ShowError(position, label, "Invalid comparation Value Type"); return; } break; case SerializedPropertyType.Enum: object paramEnum = attribute.comparationValue; object[] paramEnumArray = attribute.comparationValueArray; if (paramEnum == null && paramEnumArray == null) { ShowError(position, label, "The comparation enum value is null"); return; } else if (IsEnum(paramEnum)) { if (!CheckSameEnumType(new[] { paramEnum.GetType() }, property.serializedObject.targetObject.GetType(), conditionField.name)) { ShowError(position, label, "Enum Types doesn't match"); return; } else { string enumValue = Enum.GetValues(paramEnum.GetType()).GetValue(conditionField.enumValueIndex).ToString(); if (paramEnum.ToString() != enumValue) { showField = false; } else { showField = true; } } } else if (IsEnum(paramEnumArray)) { if (!CheckSameEnumType(paramEnumArray.Select(x => x.GetType()), property.serializedObject.targetObject.GetType(), conditionField.name)) { ShowError(position, label, "Enum Types doesn't match"); return; } else { string enumValue = Enum.GetValues(paramEnumArray[0].GetType()).GetValue(conditionField.enumValueIndex).ToString(); if (paramEnumArray.All(x => x.ToString() != enumValue)) { showField = false; } else { showField = true; } } } else { ShowError(position, label, "The comparation enum value is not an enum"); return; } break; case SerializedPropertyType.Integer: case SerializedPropertyType.Float: string stringValue; bool error = false; float conditionValue = 0; if (conditionField.propertyType == SerializedPropertyType.Integer) { conditionValue = conditionField.intValue; } else if (conditionField.propertyType == SerializedPropertyType.Float) { conditionValue = conditionField.floatValue; } try { stringValue = (string)attribute.comparationValue; } catch { ShowError(position, label, "Invalid comparation Value Type"); return; } if (stringValue.StartsWith("==")) { float?value = GetValue(stringValue, "=="); if (value == null) { error = true; } else { showField = conditionValue == value; } } else if (stringValue.StartsWith("!=")) { float?value = GetValue(stringValue, "!="); if (value == null) { error = true; } else { showField = conditionValue != value; } } else if (stringValue.StartsWith("<=")) { float?value = GetValue(stringValue, "<="); if (value == null) { error = true; } else { showField = conditionValue <= value; } } else if (stringValue.StartsWith(">=")) { float?value = GetValue(stringValue, ">="); if (value == null) { error = true; } else { showField = conditionValue >= value; } } else if (stringValue.StartsWith("<")) { float?value = GetValue(stringValue, "<"); if (value == null) { error = true; } else { showField = conditionValue < value; } } else if (stringValue.StartsWith(">")) { float?value = GetValue(stringValue, ">"); if (value == null) { error = true; } else { showField = conditionValue > value; } } if (error) { ShowError(position, label, "Invalid comparation instruction for Int or float value"); return; } break; default: ShowError(position, label, "This type has not supported."); return; } if (showField) { EditorGUI.PropertyField(position, property, true); } }
bool ShouldShowField(SerializedProperty property, out bool hasError, out string errorMessage) { bool showField = true; ShowWhenAttribute attribute = (ShowWhenAttribute)this.attribute; hasError = false; errorMessage = ""; //SerializedProperty conditionField = property.serializedObject.FindProperty(attribute.conditionFieldName); SerializedProperty conditionField = property.FindSiblingProperty(attribute.conditionFieldName); // We check that exist a Field with the parameter name if (conditionField == null) { hasError = true; errorMessage = "Error getting the condition Field. Check the name."; return(true);// Errors should be displayed } switch (conditionField.propertyType) { case SerializedPropertyType.Boolean: try { bool comparationValue = attribute.comparationValue == null || (bool)attribute.comparationValue; showField = conditionField.boolValue == comparationValue; } catch { hasError = true; errorMessage = "Invalid comparation Value Type"; return(true); // Errors should be displayed } break; case SerializedPropertyType.Enum: object paramEnum = attribute.comparationValue; object[] paramEnumArray = attribute.comparationValueArray; if (paramEnum == null && paramEnumArray == null) { hasError = true; errorMessage = "The comparation enum value is null"; return(true); // Errors should be displayed } else if (IsEnum(paramEnum)) { if (!CheckSameEnumType(new[] { paramEnum.GetType() }, property.serializedObject.targetObject.GetType(), conditionField.propertyPath)) { hasError = true; errorMessage = "Enum Types doesn't match"; return(true); // Errors should be displayed } else { string enumValue = Enum.GetValues(paramEnum.GetType()).GetValue(conditionField.enumValueIndex).ToString(); if (paramEnum.ToString() != enumValue) { showField = false; } else { showField = true; } } } else if (IsEnum(paramEnumArray)) { if (!CheckSameEnumType(paramEnumArray.Select(x => x.GetType()), property.serializedObject.targetObject.GetType(), conditionField.propertyPath)) { hasError = true; errorMessage = "Enum Types doesn't match"; return(true); // Errors should be displayed } else { string enumValue = Enum.GetValues(paramEnumArray[0].GetType()).GetValue(conditionField.enumValueIndex).ToString(); if (paramEnumArray.All(x => x.ToString() != enumValue)) { showField = false; } else { showField = true; } } } /*else if (paramEnum.GetType() == typeof(string)) * { * string notEnum = paramEnum.ToString().Remove(0, 1); * if (string.IsNullOrWhiteSpace(notEnum)) * { * hasError = true; * errorMessage = "The comparation enum value is empty, whitespace, or null"; * return true; * } * string enumValue = Enum.GetValues(conditionField.GetTheActualType()).GetValue(conditionField.enumValueIndex).ToString(); * if (enumValue == notEnum) * showField = false; * else * showField = true; * }*/ else { hasError = true; errorMessage = "The comparation enum value is not an enum"; return(true); // Errors should be displayed } break; case SerializedPropertyType.Integer: case SerializedPropertyType.Float: string stringValue; bool error = false; float conditionValue = 0; if (conditionField.propertyType == SerializedPropertyType.Integer) { conditionValue = conditionField.intValue; } else if (conditionField.propertyType == SerializedPropertyType.Float) { conditionValue = conditionField.floatValue; } try { stringValue = (string)attribute.comparationValue; } catch { hasError = true; errorMessage = "Invalid comparation Value Type"; return(true); // Errors should be displayed } if (stringValue.StartsWith("==")) { float?value = GetValue(stringValue, "=="); if (value == null) { error = true; } else { showField = conditionValue == value; } } else if (stringValue.StartsWith("!=")) { float?value = GetValue(stringValue, "!="); if (value == null) { error = true; } else { showField = conditionValue != value; } } else if (stringValue.StartsWith("<=")) { float?value = GetValue(stringValue, "<="); if (value == null) { error = true; } else { showField = conditionValue <= value; } } else if (stringValue.StartsWith(">=")) { float?value = GetValue(stringValue, ">="); if (value == null) { error = true; } else { showField = conditionValue >= value; } } else if (stringValue.StartsWith("<")) { float?value = GetValue(stringValue, "<"); if (value == null) { error = true; } else { showField = conditionValue < value; } } else if (stringValue.StartsWith(">")) { float?value = GetValue(stringValue, ">"); if (value == null) { error = true; } else { showField = conditionValue > value; } } if (error) { hasError = true; errorMessage = "Invalid comparation instruction for Int or float value"; return(true); // Errors should be displayed } break; default: hasError = true; errorMessage = "This type has not supported."; return(true); // Errors should be displayed } return(showField); }
private bool IsPropertyVisible(SerializedProperty prop, ShowWhenAttribute attrib) { if (prop.propertyType == SerializedPropertyType.Boolean && attrib.type == ShowWhenAttribute.Type.Boolean) { switch (attrib.condition) { case ShowWhenAttribute.Condition.Equals: return(prop.boolValue == attrib.boolValue); default: return(prop.boolValue != attrib.boolValue); } } else if ( prop.propertyType == SerializedPropertyType.Float && attrib.type == ShowWhenAttribute.Type.Float) { switch (attrib.condition) { case ShowWhenAttribute.Condition.Equals: return(prop.floatValue == attrib.floatValue); case ShowWhenAttribute.Condition.Greater: return(prop.floatValue > attrib.floatValue); case ShowWhenAttribute.Condition.Less: return(prop.floatValue < attrib.floatValue); case ShowWhenAttribute.Condition.NotEquals: return(prop.floatValue != attrib.floatValue); } } else if ( prop.propertyType == SerializedPropertyType.Integer && attrib.type == ShowWhenAttribute.Type.Integer) { switch (attrib.condition) { case ShowWhenAttribute.Condition.Equals: return(prop.intValue == attrib.intValue); case ShowWhenAttribute.Condition.Greater: return(prop.intValue > attrib.intValue); case ShowWhenAttribute.Condition.Less: return(prop.intValue < attrib.intValue); case ShowWhenAttribute.Condition.NotEquals: return(prop.intValue != attrib.intValue); } } else if ( prop.propertyType == SerializedPropertyType.Enum && attrib.type == ShowWhenAttribute.Type.Integer) { switch (attrib.condition) { case ShowWhenAttribute.Condition.Equals: return(prop.enumValueIndex == attrib.intValue); case ShowWhenAttribute.Condition.Greater: return(prop.enumValueIndex > attrib.intValue); case ShowWhenAttribute.Condition.Less: return(prop.enumValueIndex < attrib.intValue); case ShowWhenAttribute.Condition.NotEquals: return(prop.enumValueIndex != attrib.intValue); } } else if ( prop.propertyType == SerializedPropertyType.String && (attrib.type == ShowWhenAttribute.Type.String || attrib.type == ShowWhenAttribute.Type.Boolean)) { if (attrib.type == ShowWhenAttribute.Type.String) { switch (attrib.condition) { case ShowWhenAttribute.Condition.Equals: return(prop.stringValue == attrib.stringValue); case ShowWhenAttribute.Condition.Greater: case ShowWhenAttribute.Condition.Less: case ShowWhenAttribute.Condition.NotEquals: return(prop.stringValue != attrib.stringValue); } } else { return(prop.stringValue.Length > 0); } } else if ( prop.propertyType == SerializedPropertyType.ObjectReference && (attrib.type == ShowWhenAttribute.Type.Object || attrib.type == ShowWhenAttribute.Type.Boolean)) { if (attrib.type == ShowWhenAttribute.Type.Object) { switch (attrib.condition) { case ShowWhenAttribute.Condition.Equals: return(prop.objectReferenceValue == attrib.objectValue); case ShowWhenAttribute.Condition.NotEquals: return(prop.objectReferenceValue != attrib.objectValue); default: return(false); } } else { return(prop.objectReferenceValue != null); } } return(true); }
public bool CanDraw(CyberAttribute cyberAttrribute) { ShowWhenAttribute atr = cyberAttrribute as ShowWhenAttribute; return(TheEditor.CheckEquals(atr)); }
public bool CanDraw(CyberAttrribute cyberAttrribute) { ShowWhenAttribute atr = cyberAttrribute as ShowWhenAttribute; return(TheEditor.CheckEquals(CyberEdit.Current.CurrentProp.serializedObject.FindProperty(atr.Prop), atr.Value, atr.Equaler)); }