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); } } }
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]"; WarningsPool.Log(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); }
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.Log(warning, property.serializedObject.targetObject); }
private static void CheckForUpdates() { MyEditorEvents.OnEditorStarts -= CheckForUpdates; MyBoxUtilities.GetMyBoxLatestVersionAsync(version => { _installedVersion = MyBoxUtilities.GetMyBoxInstalledVersion(); _latestVersion = version; if (!_installedVersion.VersionsMatch(_latestVersion)) { var versions = "Installed version: " + _installedVersion.AsSting + ". Latest version: " + _latestVersion.AsSting; var message = "It's time to update MyBox :)! Use \"Tools/MyBox/Update MyBox\". " + versions; WarningsPool.Log(message); } }); }
static MyBoxWindow() { if (AutoUpdateCheckIsEnabled) { MyBoxUtilities.GetMyBoxLatestVersionAsync(version => { _installedVersion = MyBoxUtilities.GetMyBoxInstalledVersion(); _latestVersion = version; if (!_installedVersion.VersionsMatch(_latestVersion)) { var versions = "Installed version: " + _installedVersion.AsSting + ". Latest version: " + _latestVersion.AsSting; var message = "It's time to update MyBox :)! Use \"Tools/MyBox/Update MyBox\". " + versions; WarningsPool.Log(message); } }); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (property.isArray) { WarningsPool.LogCollectionsNotSupportedWarning(property, nameof(OverrideLabelAttribute)); } label.text = ((OverrideLabelAttribute)attribute).NewLabel; var customDrawer = CustomDrawerUtility.GetPropertyDrawerForProperty(property, fieldInfo, attribute); if (customDrawer != null) { customDrawer.OnGUI(position, property, label); } else { EditorGUI.PropertyField(position, property, label, true); } }
/// <summary> /// Get the other Property which is stored alongside with specified Property, by name /// </summary> private 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); WarningsPool.LogCollectionsNotSupportedWarning(arrayProp, nameof(ConditionalFieldAttribute)); return(null); } return(nestedProperty.FindPropertyRelative(propertyName)); }
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.Log("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.Log("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);