/// <summary> /// /// </summary> /// <param name="property"></param> /// <returns> True on error. True if the condition is met. False if the condition is not met </returns> public bool CheckCondition(SerializedProperty property) { drawIfAttribute = (DrawIfAttribute)attribute; string path = drawIfAttribute.comparedPropertyName; comparedField = property.serializedObject.FindProperty(path); if (comparedField == null) { Debug.LogError("Cannot find property with name: " + path); return(true); } switch (comparedField.type) // Possible extend cases to support your own type { case "bool": return(comparedField.boolValue.Equals(drawIfAttribute.comparedValue)); case "Enum": return(comparedField.enumValueIndex.Equals((int)drawIfAttribute.comparedValue)); default: Debug.LogError("Error: " + comparedField.type + " is not supported of " + path); return(true); } }
/// <summary> /// Errors default to showing the property. /// </summary> private bool ShowMe(SerializedProperty property) { drawIf = attribute as DrawIfAttribute; // Replace propertyname to the value from the parameter string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, drawIf.comparedPropertyName) : drawIf.comparedPropertyName; comparedField = property.serializedObject.FindProperty(path); if (comparedField == null) { Debug.LogError("Cannot find property with name: " + path); return(true); } // get the value & compare based on types switch (comparedField.type) { // Possible extend cases to support your own type case "bool": return(comparedField.boolValue.Equals(drawIf.comparedValue)); case "Enum": return(comparedField.enumValueIndex.Equals((int)drawIf.comparedValue)); default: Debug.LogError("Error: " + comparedField.type + " is not supported of " + path); return(true); } }
/// <summary> /// Errors default to showing the property. /// </summary> private bool ShowMe(SerializedProperty pProperty) { _drawIf = attribute as DrawIfAttribute; // Replace propertyname to the value from the parameter string path = pProperty.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(pProperty.propertyPath, _drawIf.ComparedPropertyName) : _drawIf.ComparedPropertyName; _comparedField = pProperty.serializedObject.FindProperty(path); if (_comparedField == null) { Debug.LogError("Cannot find property with name: " + path); return(true); } if (_comparedField.isArray) { int relativeIntVal = _comparedField.arraySize.CompareTo((int)_drawIf.ComparedValue); return(_drawIf.Comparison switch { ComparisonType.Equals => relativeIntVal == 0, ComparisonType.NotEqual => relativeIntVal != 0, ComparisonType.GreaterThan => relativeIntVal == 1, ComparisonType.SmallerThan => relativeIntVal == -1, ComparisonType.SmallerOrEqual => relativeIntVal == -1 || relativeIntVal == 0, ComparisonType.GreaterOrEqual => relativeIntVal == 1 || relativeIntVal == 0, _ => true, });
// Errors default to Showing the Property. private bool ShowMe(SerializedProperty property) { drawIf = attribute as DrawIfAttribute; // Get the Serialized Parameter string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, drawIf.comparedPropertyName) : drawIf.comparedPropertyName; comparedField = property.serializedObject.FindProperty(path); // No Parameter Found if (comparedField == null) { Debug.LogError("Cannot find property with name: " + path); return(true); } // Get Value of Serialized Parameter & Compare - Supports Bool & Enum Currently switch (comparedField.type) { case "bool": return(comparedField.boolValue.Equals(drawIf.comparedValue)); case "Enum": return(comparedField.enumValueIndex.Equals((int)drawIf.comparedValue)); default: Debug.LogError("Error: " + comparedField.type + " is not supported of " + path); return(true); } }
/// <summary> /// Errors default to showing the property. /// </summary> private bool ShowMe(SerializedProperty property) { drawIf = attribute as DrawIfAttribute; // заменим имя свойства на значение из параметра string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, drawIf.comparedPropertyName) : drawIf.comparedPropertyName; comparedField = property.serializedObject.FindProperty(path); if (comparedField == null) { Debug.LogError("Cannot find property with name: " + path); return(true); } // получить значение и сравнить его по типу switch (comparedField.type) { // Возможные варианты расширения для поддержки собственного типа case "bool": return(comparedField.boolValue.Equals(drawIf.comparedValue)); case "Enum": return(comparedField.enumValueIndex.Equals((int)drawIf.comparedValue)); default: Debug.LogError("Error: " + comparedField.type + " is not supported of " + path); return(true); } }
private bool ShouldBeShown(SerializedProperty property) { drawIf = attribute as DrawIfAttribute; string path = property.propertyPath.Contains(".") ? Path.ChangeExtension(property.propertyPath, drawIf.ComparedPropertyName) : drawIf.ComparedPropertyName; comparedField = property.serializedObject.FindProperty(path); if (comparedField == null) { Debug.LogError("Cannot find property with name: " + path); return(true); } switch (comparedField.type) { case "bool": return(comparedField.boolValue.Equals(drawIf.ComparedValue)); case "Enum": return(comparedField.enumValueIndex.Equals((int)drawIf.ComparedValue)); default: Debug.LogError("Error: " + comparedField.type + " is not supported of " + path); return(true); } }
/// <summary> /// Errors default to showing the property. /// </summary> private bool ShowMe(SerializedProperty property) { drawIf = attribute as DrawIfAttribute; // Replace propertyname to the value from the parameter string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, drawIf.comparedPropertyName) : drawIf.comparedPropertyName; comparedField = property.serializedObject.FindProperty(path); if (comparedField == null) { Debug.LogError("Cannot find property with name: " + path); return(true); } if (comparedField.isArray) { var array = comparedField.Copy(); array.Next(true); // skip generic field array.Next(true); // advance to array size field // Get the array size var arrayLength = array.intValue; array.Next(true); // advance to first array index // Write values to list var values = new List <int>(arrayLength); int lastIndex = arrayLength - 1; for (int i = 0; i < arrayLength; i++) { values.Add(array.intValue); // copy the value to the list if (i < lastIndex) { array.Next(false); // advance without drilling into children } } return(values.Contains((int)drawIf.comparedValue)); } // get the value & compare based on types switch (comparedField.type) { // Possible extend cases to support your own type case "bool": return(comparedField.boolValue.Equals(drawIf.comparedValue)); case "Enum": return(comparedField.enumValueIndex.Equals((int)drawIf.comparedValue)); default: Debug.LogError("Error: " + comparedField.type + " is not supported of " + path); return(true); } }
/// <summary> /// Errors default to showing the property. /// </summary> private bool ShowMe(SerializedProperty property) { drawIf = attribute as DrawIfAttribute; // Replace property name to the value from the parameter foreach (var copareData in drawIf.m_CompareList) { string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, copareData.m_PropertyName) : copareData.m_PropertyName; comparedField = property.serializedObject.FindProperty(path); if (comparedField == null) { Debug.LogError("Cannot find property with name: " + path); return(true); } // get the value & compare based on types switch (comparedField.propertyType) { // Possible extend cases to support your own type case SerializedPropertyType.Boolean: if (comparedField.boolValue.Equals(copareData.m_PropertyValue)) { return(true); } break; case SerializedPropertyType.Enum: if (comparedField.enumValueIndex.Equals((int)copareData.m_PropertyValue)) { return(true); } break; case SerializedPropertyType.ObjectReference: if (comparedField.objectReferenceValue != null) { return(true); } break; default: Debug.LogError("Error: " + comparedField.propertyType + " is not supported of " + path); return(true); } } return(false); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { drawIf = attribute as DrawIfAttribute; string[] nested = drawIf.ComparedPropertyName.Split('/'); string mainName = drawIf.ComparedPropertyName; SerializedProperty temporal = property; for (int i = 0; i < nested.Length; i++) { mainName = nested[i]; temporal = GetTargetPropertyFromGivenChild(temporal, mainName); } comparedField = temporal; if (comparedField == null) { Debug.LogWarning("Not found compared field : " + drawIf.ComparedPropertyName); return; } object comparedFieldValue = EditorUtils.GetTargetObjectOfProperty(comparedField); double comparedFieldNumber = 0; bool isConditionMet = false; // todo should be better way to check if it's numeric, this can fail later with equality of integers etc. if (!double.TryParse(Convert.ToString(drawIf.ComparedValue, CultureInfo.InvariantCulture), NumberStyles.Any, NumberFormatInfo.InvariantInfo, out double comparedNumber) || !double.TryParse(Convert.ToString(comparedFieldValue, CultureInfo.InvariantCulture), NumberStyles.Any, NumberFormatInfo.InvariantInfo, out comparedFieldNumber)) { if (drawIf.ComparisonType != ComparisonType.Equals && drawIf.ComparisonType != ComparisonType.NotEquals) { Debug.LogError("The only comparison types available to type '" + comparedFieldValue.GetType() + "' are Equals and NotEqual. (On object '" + property.serializedObject.targetObject.name + "')"); return; } } if (comparedFieldValue == null) { Debug.LogWarning("Not found compared field value"); return; } switch (drawIf.ComparisonType) { case ComparisonType.Equals: if (comparedFieldValue.Equals(drawIf.ComparedValue)) { isConditionMet = true; } break; case ComparisonType.NotEquals: if (!comparedFieldValue.Equals(drawIf.ComparedValue)) { isConditionMet = true; } break; case ComparisonType.GreaterOrEquals: if (comparedFieldNumber >= comparedNumber) { isConditionMet = true; } break; case ComparisonType.SmallerOrEquals: if (comparedFieldNumber <= comparedNumber) { isConditionMet = true; } break; case ComparisonType.GreaterThan: if (comparedFieldNumber > comparedNumber) { isConditionMet = true; } break; case ComparisonType.SmallerThan: if (comparedFieldNumber < comparedNumber) { isConditionMet = true; } break; } propertyHeight = base.GetPropertyHeight(property, label); if (isConditionMet) { EditorGUI.PropertyField(position, property); } else { if (drawIf.DisablingType == DisablingType.ReadOnly) { GUI.enabled = false; EditorGUI.PropertyField(position, property); GUI.enabled = true; } else { propertyHeight = 0f; } } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { // Set the global variables. drawIf = attribute as DrawIfAttribute; 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 == DisablingType.ReadOnly) { GUI.enabled = false; EditorGUI.PropertyField(position, property); GUI.enabled = true; } else { propertyHeight = 0f; } } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { attr = attribute as DrawIfAttribute; object value = GetValue(attr.type, property.serializedObject.FindProperty(attr.name)); bool active = false; if (attr.comparisonType == ComparisonType.Equals) { if (value.Equals(attr.value)) { active = true; } } else if (attr.comparisonType == ComparisonType.NotEqual) { if (!value.Equals(attr.value)) { active = true; } } else if (IsNumericType(attr.type)) { double a = 0, b = 0; if (IsInt(attr.type)) { a = (int)value; b = (int)attr.value; } else if (IsLong(attr.type)) { a = (long)value; b = (long)attr.value; } else if (IsFloat(attr.type)) { a = (float)value; b = (float)attr.value; } else if (IsDouble(attr.type)) { a = (double)value; b = (double)attr.value; } switch (attr.comparisonType) { case ComparisonType.GreaterThan: if (a > b) { active = true; } break; case ComparisonType.SmallerThan: if (a < b) { active = true; } break; case ComparisonType.SmallerOrEqual: if (a <= b) { active = true; } break; case ComparisonType.GreaterOrEqual: if (a >= b) { active = true; } break; } } propertyHeight = base.GetPropertyHeight(property, label); if (active) { EditorGUI.PropertyField(position, property); } else { if (attr.disablingType == DisablingType.ReadOnly) { GUI.enabled = false; EditorGUI.PropertyField(position, property); GUI.enabled = true; } else { propertyHeight = 0f; } } }
/// <summary> /// Errors default to showing the property. /// </summary> private bool ShowMe(SerializedProperty property) { drawIf = attribute as DrawIfAttribute; // Replace propertyname to the value from the parameter string path = property.propertyPath.Contains(".") ? System.IO.Path.ChangeExtension(property.propertyPath, drawIf.comparedPropertyName) : drawIf.comparedPropertyName; comparedField = property.serializedObject.FindProperty(path); if (comparedField == null) { Debug.LogError("Cannot find property with name: " + path); return(true); } switch (drawIf.comparisonType) { case ComparisonType.Equals: switch (comparedField.type) { case "float": return(comparedField.floatValue.Equals(drawIf.comparedValue)); case "int": return(comparedField.intValue.Equals(drawIf.comparedValue)); case "bool": return(comparedField.boolValue.Equals(drawIf.comparedValue)); case "Enum": return(comparedField.enumValueIndex.Equals((int)drawIf.comparedValue)); default: return(true); } case ComparisonType.NotEqual: switch (comparedField.type) { case "float": return(!comparedField.floatValue.Equals(drawIf.comparedValue)); case "int": return(!comparedField.intValue.Equals(drawIf.comparedValue)); case "bool": return(!comparedField.boolValue.Equals(drawIf.comparedValue)); case "Enum": return(!comparedField.enumValueIndex.Equals((int)drawIf.comparedValue)); default: return(true); } case ComparisonType.GreaterThan: switch (comparedField.type) { case "float": return(comparedField.floatValue > (float)drawIf.comparedValue); case "int": return(comparedField.intValue > (int)drawIf.comparedValue); default: return(true); } case ComparisonType.SmallerThan: switch (comparedField.type) { case "Float": return(comparedField.floatValue < (float)drawIf.comparedValue); case "int": return(comparedField.intValue < (int)drawIf.comparedValue); default: return(true); } case ComparisonType.IsNull: return(drawIf.comparedValue == null); default: return(true); } }
private string ShowLabelIfComparedValueIsEnum(string label) { drawIf = attribute as DrawIfAttribute; return($"*{drawIf.comparedValue}*: {label}"); }