public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // get the attribute data ConditionalHideAttribute condHAtt = (ConditionalHideAttribute)attribute; // enabled / disable the property bool enabled = GetConditionalHideAttributeResult(condHAtt, property); bool wasEnabled = GUI.enabled; // check if we should draw the property if (!condHAtt.HideInInspector || enabled) { EditorGUI.PropertyField(position, property, label, true); } // Ensure that the next property that is being drawn uses the correct settings GUI.enabled = wasEnabled; }
private bool GetConditionalHideAttributeResult(ConditionalHideAttribute condHAtt, SerializedProperty property) { bool enabled = true; //Look for the sourcefield within the object that the property belongs to SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField); if (sourcePropertyValue != null) { enabled = sourcePropertyValue.boolValue; } else { Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } return(enabled); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { ConditionalHideAttribute condHAtt = (ConditionalHideAttribute)attribute; bool enabled = GetConditionalHideAttributeResult(condHAtt, property); bool wasEnabled = GUI.enabled; GUI.enabled = enabled; if (!condHAtt.HideInInspector || enabled) { Rect rect = position; rect.x += 14; rect.width -= 14; EditorGUI.PropertyField(rect, property, label, true); } GUI.enabled = wasEnabled; }
private bool GetConditionalHideAttributeResult(ConditionalHideAttribute condHAtt, SerializedProperty property) { bool enabled = true; string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField); //changes the path to the conditionalsource property path SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(conditionPath); if (sourcePropertyValue != null) { enabled = sourcePropertyValue.boolValue; } else { Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } return(enabled); }
//Fetch the properties that are suppposed to show, and check whether the stuff contained should be showing. If so, display them. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { //Cache the attribute and get the hideininspector variable ConditionalHideAttribute condHAtt = (ConditionalHideAttribute)attribute; bool enabled = GetConditionalHideAttributeResult(condHAtt, property); //Enable the GUI Element if HideInInspector != true bool wasEnabled = GUI.enabled; GUI.enabled = enabled; if (!condHAtt.HideInInspector || enabled) { EditorGUI.PropertyField(position, property, label, true); } //Reflect the enable status on the GUI Element GUI.enabled = wasEnabled; }
public static string GetConditionPath(ConditionalHideAttribute condHAtt, SerializedProperty property) { //Get the full relative property path of the sourcefield so we can have nested hiding string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField); //changes the path to the conditionalsource property path if (property.name == "data") { string[] path = propertyPath.Split('.'); if (path[path.Length - 2] == "Array") { conditionPath = propertyPath.Remove(propertyPath.Length - (".Array.data[*]".Length + path[path.Length - 3].Length)) + condHAtt.ConditionalSourceField; //special case for arrays } } return(conditionPath); }
private bool CheckPropertyType(SerializedProperty sourcePropertyValue) { ConditionalHideAttribute condHAtt = (ConditionalHideAttribute)attribute; switch (sourcePropertyValue.propertyType) { case SerializedPropertyType.Boolean: return(sourcePropertyValue.boolValue); case SerializedPropertyType.ObjectReference: return(sourcePropertyValue.objectReferenceValue != null); case SerializedPropertyType.Enum: return(condHAtt.enumIndex == sourcePropertyValue.enumValueIndex); default: Debug.LogError("Data type of the property used for conditional hiding [" + sourcePropertyValue.propertyType + "] is currently not supported"); return(true); } }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { /* Get Attribute Data */ ConditionalHideAttribute cha = (ConditionalHideAttribute)attribute; /* Get if Property is True or False */ bool enabled = GetConditionalHideAttributeResult(cha, property); /* Return Property Height of Property if NOT Hidden */ if (cha.hideInInspector != enabled) { return(EditorGUI.GetPropertyHeight(property, label)); } /* Else, Return the Default Vertical Spacing */ else { return(-EditorGUIUtility.standardVerticalSpacing); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { /*Get Attribute Data */ ConditionalHideAttribute cha = (ConditionalHideAttribute)attribute; /* Get if Property is True or False */ bool enabled = GetConditionalHideAttributeResult(cha, property); /* Draw the Property If NOT Hidden */ if (cha.hideInInspector != enabled) { if (cha.min == cha.max) { EditorGUI.PropertyField(position, property, label, true); } else { EditorGUI.Slider(position, property, cha.min, cha.max); } } }
private bool GetConditionalHideAttributeResult(ConditionalHideAttribute condHAtt, SerializedProperty property) { bool enabled = false; //Get the full relative property path of the sourcefield so we can have nested hiding string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField); //changes the path to the conditionalsource property path SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(conditionPath); //SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField); if (sourcePropertyValue != null) { enabled = CheckPropertyType(sourcePropertyValue); } else { //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField2); //changes the path to the conditionalsource property path SerializedProperty sourcePropertyValue2 = property.serializedObject.FindProperty(conditionPath); //SerializedProperty sourcePropertyValue2 = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField2); if (sourcePropertyValue2 != null) { enabled = enabled && CheckPropertyType(sourcePropertyValue2); } else { //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } if (condHAtt.Inverse) { enabled = !enabled; } return(enabled); }
private bool GetConditionalHideAttributeResult(ConditionalHideAttribute condHAtt, SerializedProperty property) { bool enabled = false; string conditionPath = GetConditionPath(condHAtt, property); SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(conditionPath); if (sourcePropertyValue != null) { enabled = CheckPropertyType(sourcePropertyValue); } else { object propertyParent = GetParentObject(property); PropertyInfo info = propertyParent.GetType().GetProperty(condHAtt.ConditionalSourceField); if (info == null) { propertyParent = property.serializedObject.targetObject; info = propertyParent.GetType().GetProperty(condHAtt.ConditionalSourceField); } if (info != null) { enabled = (bool)(info.GetValue(propertyParent, new object[0])); } else { Debug.LogError(condHAtt.ConditionalSourceField + " was not found."); } } if (condHAtt.Inverse) { enabled = !enabled; } return(enabled); }
private bool GetConditionalHideAttributeResult(ConditionalHideAttribute condHAtt, SerializedProperty property) { bool enabled = true; SerializedProperty myProp = property; // Check if array string path = property.propertyPath; int arrayIndex = path.LastIndexOf(".Array"); bool isArray = arrayIndex >= 0; if (isArray) { // If we're dealing with an array then we need to get the object and find the array property so we can mess with it SerializedObject so = property.serializedObject; string arrayPath = path.Substring(0, arrayIndex); SerializedProperty arrayProp = so.FindProperty(arrayPath); myProp = arrayProp; } // Returns the property path of the property we want to apply the attribute to string propertyPath = myProp.propertyPath; // Changes the path to the conditionalsource property path string conditionPath = propertyPath.Replace(myProp.name, condHAtt.ConditionalSourceField); SerializedProperty sourcePropertyValue = myProp.serializedObject.FindProperty(conditionPath); if (sourcePropertyValue != null) { enabled = sourcePropertyValue.boolValue; } else { Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } return(enabled); }
private bool GetConditionalHideAttributeResult(ConditionalHideAttribute condHAtt, SerializedProperty property) { bool enabled = true; SerializedProperty sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField); if (sourcePropertyValue != null) { enabled = CheckPropertyType(sourcePropertyValue); } else { Debug.LogWarning( "Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } SerializedProperty sourcePropertyValue2 = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField2); if (sourcePropertyValue2 != null) { enabled = enabled && CheckPropertyType(sourcePropertyValue2); } else { //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } if (condHAtt.Inverse) { enabled = !enabled; } return(enabled); }
bool GetConditionalHideAttributeResult(ConditionalHideAttribute condHAtt, SerializedProperty property) { SerializedProperty sourcePropertyValue = null; //Get the full relative property path of the sourcefield so we can have nested hiding.Use old method when dealing with arrays if (!property.isArray) { string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, condHAtt.conditionalSourceField); //changes the path to the conditionalsource property path sourcePropertyValue = property.serializedObject.FindProperty(conditionPath); //if the find failed->fall back to the old system if (sourcePropertyValue == null) { //original implementation (doens't work with nested serializedObjects) sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.conditionalSourceField); } } else { //original implementation (doens't work with nested serializedObjects) sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.conditionalSourceField); } if (sourcePropertyValue != null) { return(CheckPropertyType(condHAtt, sourcePropertyValue)); } return(true); }
bool FulfillsCondition(SerializedProperty property) { ConditionalHideAttribute hideAttribute = (ConditionalHideAttribute)attribute; int index = property.propertyPath.LastIndexOf('.'); string containerPath = property.propertyPath.Substring(0, index + 1); string conditionPath = containerPath + hideAttribute.ConditionalFieldPath; SerializedProperty conditionalProperty = property.serializedObject.FindProperty(conditionPath); if (conditionalProperty.type == "Enum") { return(conditionalProperty.enumValueIndex == (int)hideAttribute.HideValue); } if (conditionalProperty.type == "int") { return(conditionalProperty.intValue == (int)hideAttribute.HideValue); } if (conditionalProperty.type == "float") { return(conditionalProperty.floatValue == (float)hideAttribute.HideValue); } if (conditionalProperty.type == "string") { return(conditionalProperty.stringValue == (string)hideAttribute.HideValue); } if (conditionalProperty.type == "double") { return(conditionalProperty.doubleValue == (double)hideAttribute.HideValue); } if (conditionalProperty.type == "bool") { return(conditionalProperty.boolValue == (bool)hideAttribute.HideValue); } return(conditionalProperty.objectReferenceValue == (object)hideAttribute.HideValue); }
private bool GetConditionalHideAttributeResult(ConditionalHideAttribute condHAtt, SerializedProperty property) { bool enabled = (condHAtt.UseOrLogic) ?false :true; //Handle primary property SerializedProperty sourcePropertyValue = null; //Get the full relative property path of the sourcefield so we can have nested hiding.Use old method when dealing with arrays if (!property.isArray) { string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField); //changes the path to the conditionalsource property path sourcePropertyValue = property.serializedObject.FindProperty(conditionPath); //if the find failed->fall back to the old system if (sourcePropertyValue == null) { //original implementation (doens't work with nested serializedObjects) sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField); } } else { //original implementation (doens't work with nested serializedObjects) sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField); } if (sourcePropertyValue != null) { enabled = CheckPropertyType(sourcePropertyValue); if (condHAtt.InverseCondition1) { enabled = !enabled; } } else { //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } //handle secondary property SerializedProperty sourcePropertyValue2 = null; if (!property.isArray) { string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField2); //changes the path to the conditionalsource property path sourcePropertyValue2 = property.serializedObject.FindProperty(conditionPath); //if the find failed->fall back to the old system if (sourcePropertyValue2 == null) { //original implementation (doens't work with nested serializedObjects) sourcePropertyValue2 = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField2); } } else { // original implementation(doens't work with nested serializedObjects) sourcePropertyValue2 = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField2); } //Combine the results if (sourcePropertyValue2 != null) { bool prop2Enabled = CheckPropertyType(sourcePropertyValue2); if (condHAtt.InverseCondition2) { prop2Enabled = !prop2Enabled; } if (condHAtt.UseOrLogic) { enabled = enabled || prop2Enabled; } else { enabled = enabled && prop2Enabled; } } else { //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } //Handle the unlimited property array string[] conditionalSourceFieldArray = condHAtt.ConditionalSourceFields; bool[] conditionalSourceFieldInverseArray = condHAtt.ConditionalSourceFieldInverseBools; for (int index = 0; index < conditionalSourceFieldArray.Length; ++index) { SerializedProperty sourcePropertyValueFromArray = null; if (!property.isArray) { string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, conditionalSourceFieldArray[index]); //changes the path to the conditionalsource property path sourcePropertyValueFromArray = property.serializedObject.FindProperty(conditionPath); //if the find failed->fall back to the old system if (sourcePropertyValueFromArray == null) { //original implementation (doens't work with nested serializedObjects) sourcePropertyValueFromArray = property.serializedObject.FindProperty(conditionalSourceFieldArray[index]); } } else { // original implementation(doens't work with nested serializedObjects) sourcePropertyValueFromArray = property.serializedObject.FindProperty(conditionalSourceFieldArray[index]); } //Combine the results if (sourcePropertyValueFromArray != null) { bool propertyEnabled = CheckPropertyType(sourcePropertyValueFromArray); if (conditionalSourceFieldInverseArray.Length >= (index + 1) && conditionalSourceFieldInverseArray[index]) { propertyEnabled = !propertyEnabled; } if (condHAtt.UseOrLogic) { enabled = enabled || propertyEnabled; } else { enabled = enabled && propertyEnabled; } } else { //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } } //wrap it all up if (condHAtt.Inverse) { enabled = !enabled; } return(enabled); }
private bool EvaluateCondition(ConditionalHideAttribute conditionalHideAttribute, SerializedProperty serializedProperty) { SerializedProperty conditionalSerializedProperty = serializedProperty.serializedObject.FindProperty(conditionalHideAttribute.SerializableFieldName); if (conditionalSerializedProperty != null) { object value; switch (conditionalSerializedProperty.propertyType) { case SerializedPropertyType.Boolean: { value = conditionalSerializedProperty.boolValue; break; } case SerializedPropertyType.Color: { value = conditionalSerializedProperty.colorValue; break; } case SerializedPropertyType.Enum: { value = conditionalSerializedProperty.enumValueIndex; break; } case SerializedPropertyType.Float: { value = conditionalSerializedProperty.floatValue; break; } case SerializedPropertyType.Integer: { value = conditionalSerializedProperty.intValue; break; } case SerializedPropertyType.Vector2: { value = conditionalSerializedProperty.vector2Value; break; } case SerializedPropertyType.Vector3: { value = conditionalSerializedProperty.vector3Value; break; } case SerializedPropertyType.Vector4: { value = conditionalSerializedProperty.vector4Value; break; } case SerializedPropertyType.Quaternion: { value = conditionalSerializedProperty.quaternionValue; break; } case SerializedPropertyType.String: { value = conditionalSerializedProperty.stringValue; break; } case SerializedPropertyType.ObjectReference: { value = conditionalSerializedProperty.objectReferenceValue; break; } default: { Debug.LogError( string.Format( "Data type '{0}' of the serialized property '{1}' is currently not supported. The conditional hiding of the serializable field '{2}' can not be evaluated", conditionalSerializedProperty.propertyType, conditionalHideAttribute.SerializableFieldName, serializedProperty.name)); return(true); } } return(object.Equals(value, conditionalHideAttribute.SerializableFieldValue)); } Debug.LogError( string.Format( "The serialized property '{0}' was not found in the serialized object. The conditional hiding of the serializable field '{1}' can not be evaluated", conditionalHideAttribute.SerializableFieldName, serializedProperty.name)); return(true); }
private bool GetConditionalHideAttributeResult(ConditionalHideAttribute condHAtt, SerializedProperty property) { bool enabled = false; SerializedProperty sourcePropertyValue = null; //Get the full relative property path of the sourcefield so we can have nested hiding if (!property.isArray) { string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField); //changes the path to the conditionalsource property path sourcePropertyValue = property.serializedObject.FindProperty(conditionPath); //if the find failed->fall back to the old system if (sourcePropertyValue == null) { //original implementation (doens't work with nested serializedObjects) sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField); } } else { //original implementation (doens't work with nested serializedObjects) sourcePropertyValue = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField); } if (sourcePropertyValue != null) { enabled = CheckPropertyType(sourcePropertyValue); if (condHAtt.InverseCondition1) { enabled = !enabled; } } else { //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } SerializedProperty sourcePropertyValue2 = null; if (!property.isArray) { string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField2); //changes the path to the conditionalsource property path sourcePropertyValue2 = property.serializedObject.FindProperty(conditionPath); } else { // original implementation(doens't work with nested serializedObjects) sourcePropertyValue2 = property.serializedObject.FindProperty(condHAtt.ConditionalSourceField2); } if (sourcePropertyValue2 != null) { bool prop2Enabled = CheckPropertyType(sourcePropertyValue2); if (condHAtt.InverseCondition2) { prop2Enabled = !prop2Enabled; } if (condHAtt.UseOrLogic) { enabled = enabled || prop2Enabled; } else { enabled = enabled && prop2Enabled; } } else { //Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField); } if (condHAtt.Inverse) { enabled = !enabled; } return(enabled); }