public static SerializedProperty FindRelativeProperty(SerializedProperty property, string propertyName) { if (property.depth == 0) { return(property.serializedObject.FindProperty(propertyName)); } var path = property.propertyPath.Replace(".Array.data[", "["); var elements = path.Split('.'); var nestedProperty = NestedPropertyOrigin(property, elements); // if nested property is null = we hit an array property if (nestedProperty == null) { var cleanPath = path.Substring(0, path.IndexOf('[')); var arrayProp = property.serializedObject.FindProperty(cleanPath); var target = arrayProp.serializedObject.targetObject; var who = "Property <color=brown>" + arrayProp.name + "</color> in object <color=brown>" + target.name + "</color> caused: "; var warning = who + "Array fields is not supported by [ConditionalFieldAttribute]. Consider to use <color=blue>CollectionWrapper</color>"; WarningsPool.LogWarning(warning, target); return(null); } return(nestedProperty.FindPropertyRelative(propertyName)); }
/// <summary> /// Create PropertyDrawer for specified property if any PropertyDrawerType for such property is found. /// FieldInfo and Attribute will be inserted in created drawer. /// </summary> public static PropertyDrawer GetPropertyDrawerForProperty(SerializedProperty property, FieldInfo fieldInfo, Attribute attribute) { var propertyId = property.GetUniquePropertyId(); if (PropertyDrawersCache.TryGetValue(propertyId, out var drawer)) { return(drawer); } var targetType = fieldInfo.FieldType; var drawerType = GetPropertyDrawerTypeForFieldType(targetType); if (drawerType != null) { drawer = InstantiatePropertyDrawer(drawerType, fieldInfo, attribute); if (drawer == null) { WarningsPool.LogWarning(property, $"Unable to instantiate CustomDrawer of type {drawerType} for {fieldInfo.FieldType}", property.serializedObject.targetObject); } } PropertyDrawersCache[propertyId] = drawer; return(drawer); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (!_toShow) { return; } if (!CustomDrawerUsed()) { EditorGUI.PropertyField(position, property, label, true); } bool CustomDrawerUsed() { if (_customPropertyDrawer == null) { return(false); } try { _customPropertyDrawer.OnGUI(position, property, label); return(true); } catch (Exception e) { WarningsPool.LogWarning(property, "Unable to use CustomDrawer of type " + _customPropertyDrawer.GetType() + ": " + e, property.serializedObject.targetObject); return(false); } } }
private void LogWarning(string log, SerializedProperty property) { var warning = "Property <color=brown>" + fieldInfo.Name + "</color>"; if (fieldInfo != null && fieldInfo.DeclaringType != null) { warning += " on behaviour <color=brown>" + fieldInfo.DeclaringType.Name + "</color>"; } warning += " caused: " + log; WarningsPool.LogWarning(warning, property.serializedObject.targetObject); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { SerializedProperty minProp = property.FindPropertyRelative("Min"); SerializedProperty maxProp = property.FindPropertyRelative("Max"); if (minProp == null || maxProp == null) { WarningsPool.LogWarning("MinMaxRangeAttribute used on <color=brown>" + property.name + "</color>. Must be used on types with Min and Max fields", property.serializedObject.targetObject); return; } var minValid = minProp.propertyType == SerializedPropertyType.Integer || minProp.propertyType == SerializedPropertyType.Float; var maxValid = maxProp.propertyType == SerializedPropertyType.Integer || maxProp.propertyType == SerializedPropertyType.Float; if (!maxValid || !minValid || minProp.propertyType != maxProp.propertyType) { WarningsPool.LogWarning("MinMaxRangeAttribute used on <color=brown>" + property.name + "</color>. Min and Max fields must be of int or float type", property.serializedObject.targetObject); return; } MinMaxRangeAttribute rangeAttribute = (MinMaxRangeAttribute)attribute; label = EditorGUI.BeginProperty(position, label, property); position = EditorGUI.PrefixLabel(position, label); bool isInt = minProp.propertyType == SerializedPropertyType.Integer; float minValue = isInt ? minProp.intValue : minProp.floatValue; float maxValue = isInt ? maxProp.intValue : maxProp.floatValue; float rangeMin = rangeAttribute.Min; float rangeMax = rangeAttribute.Max; const float rangeBoundsLabelWidth = 40f; var rangeBoundsLabel1Rect = new Rect(position); rangeBoundsLabel1Rect.width = rangeBoundsLabelWidth; GUI.Label(rangeBoundsLabel1Rect, new GUIContent(minValue.ToString(isInt ? "F0" : "F2"))); position.xMin += rangeBoundsLabelWidth; var rangeBoundsLabel2Rect = new Rect(position); rangeBoundsLabel2Rect.xMin = rangeBoundsLabel2Rect.xMax - rangeBoundsLabelWidth; GUI.Label(rangeBoundsLabel2Rect, new GUIContent(maxValue.ToString(isInt ? "F0" : "F2"))); position.xMax -= rangeBoundsLabelWidth; EditorGUI.BeginChangeCheck(); EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, rangeMin, rangeMax); if (EditorGUI.EndChangeCheck()) { if (isInt) { minProp.intValue = Mathf.RoundToInt(minValue); maxProp.intValue = Mathf.RoundToInt(maxValue); } else { minProp.floatValue = minValue; maxProp.floatValue = maxValue; } } EditorGUI.EndProperty(); }
public static void LogMethodNotFound(UnityEngine.Object owner, string method) => WarningsPool.LogWarning(owner, $"Conditional Attribute is trying to invoke method {method.Colored(Colors.brown)} " + "which is missing or not with a bool return type", owner);
private static void LogFieldNotFound(UnityEngine.Object owner, string field) => WarningsPool.LogWarning(owner, $"Conditional Attribute is trying to check field {field.Colored(Colors.brown)} which is not present", owner);
private static void LogFieldNotFound(SerializedProperty property, string field) => WarningsPool.LogWarning(property, $"Conditional Attribute is trying to check field {field.Colored(Colors.brown)} which is not present", property.serializedObject.targetObject);