diff --git a/Editor/AbstractPropertyDrawer.cs b/Editor/AbstractPropertyDrawer.cs index e0b5ccf..c73e9a4 100644 --- a/Editor/AbstractPropertyDrawer.cs +++ b/Editor/AbstractPropertyDrawer.cs @@ -35,7 +35,7 @@ namespace Module.Inspector.Editor if (value.drawer.Validate(value.attribute, property)) continue; - validationError += value.drawer.GetValidationError(property); + validationError += value.drawer.GetValidationError(value.attribute, property); isValid = false; } @@ -47,8 +47,8 @@ namespace Module.Inspector.Editor label.tooltip = validationError; else label.tooltip += "\n" + validationError; - - GUI.color = Color.red; + + EditorIcons.SetErrorIcon(label); } if (result.draw != null) diff --git a/Editor/Predrawers/HideLabelAttributeDrawer.cs b/Editor/Predrawers/HideLabelAttributeDrawer.cs new file mode 100644 index 0000000..d0f377b --- /dev/null +++ b/Editor/Predrawers/HideLabelAttributeDrawer.cs @@ -0,0 +1,14 @@ +using UnityEditor; +using UnityEngine; + +namespace Module.Inspector.Editor +{ + [CustomPropertyDrawer(typeof(HideLabelAttribute))] + internal sealed class HideLabelAttributeDrawer : PredrawerModifierPropertyDrawer + { + public override void Modify(PredrawerModifierPropertyAttribute attribute, SerializedProperty property, GUIContent label) + { + label.text = string.Empty; + } + } +} \ No newline at end of file diff --git a/Editor/Predrawers/HideLabelAttributeDrawer.cs.meta b/Editor/Predrawers/HideLabelAttributeDrawer.cs.meta new file mode 100644 index 0000000..3d03d8d --- /dev/null +++ b/Editor/Predrawers/HideLabelAttributeDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: df07b497331149cfb8ce6e544247cb9f +timeCreated: 1699115325 \ No newline at end of file diff --git a/Editor/Utilities/EditorIcons.cs b/Editor/Utilities/EditorIcons.cs new file mode 100644 index 0000000..eaab2d3 --- /dev/null +++ b/Editor/Utilities/EditorIcons.cs @@ -0,0 +1,39 @@ +using UnityEditor; +using UnityEngine; + +namespace Module.Inspector.Editor.Utilities +{ + internal static class EditorIcons + { + private static GUIContent CONTENT_WARNING; + private static GUIContent CONTENT_ERROR; + + private static GUIContent GetWarningIconContent() + { + if (CONTENT_WARNING == null) + CONTENT_WARNING = EditorGUIUtility.IconContent("Warning"); + + return CONTENT_WARNING; + } + + private static GUIContent GetErrorIconContent() + { + if (CONTENT_ERROR == null) + CONTENT_ERROR = EditorGUIUtility.IconContent("Error"); + + return CONTENT_ERROR; + } + + public static void SetWarningIcon(GUIContent content) + { + GUIContent errorContent = GetWarningIconContent(); + content.image = errorContent.image; + } + + public static void SetErrorIcon(GUIContent content) + { + GUIContent errorContent = GetErrorIconContent(); + content.image = errorContent.image; + } + } +} \ No newline at end of file diff --git a/Editor/Utilities/EditorIcons.cs.meta b/Editor/Utilities/EditorIcons.cs.meta new file mode 100644 index 0000000..8b30010 --- /dev/null +++ b/Editor/Utilities/EditorIcons.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 07b74dac541c43cb8a89bd0a1b577ec1 +timeCreated: 1699115532 \ No newline at end of file diff --git a/Editor/ValidatePropertyDrawer.cs b/Editor/ValidatePropertyDrawer.cs index d1f2968..9ac2e1d 100644 --- a/Editor/ValidatePropertyDrawer.cs +++ b/Editor/ValidatePropertyDrawer.cs @@ -5,6 +5,6 @@ namespace Module.Inspector.Editor public abstract class ValidatePropertyDrawer : AbstractPropertyDrawer { public abstract bool Validate(ValidatePropertyAttribute attribute, SerializedProperty property); - public abstract string GetValidationError(SerializedProperty property); + public abstract string GetValidationError(ValidatePropertyAttribute attribute, SerializedProperty property); } } \ No newline at end of file diff --git a/Editor/Validators/NotNullFieldAttributeDrawer.cs b/Editor/Validators/NotNullFieldAttributeDrawer.cs index b950b7c..0f25935 100644 --- a/Editor/Validators/NotNullFieldAttributeDrawer.cs +++ b/Editor/Validators/NotNullFieldAttributeDrawer.cs @@ -42,9 +42,14 @@ namespace Module.Inspector.Editor return true; } - public override string GetValidationError(SerializedProperty property) + public override string GetValidationError(ValidatePropertyAttribute attribute, SerializedProperty property) { - return "NotNullField: Value is null"; + string errorMessage = ((NotNullFieldAttribute)attribute).errorMessage; + + if (string.IsNullOrEmpty(errorMessage)) + return "NotNullField: Value is null"; + + return errorMessage; } } } \ No newline at end of file diff --git a/README.md b/README.md index b213b17..636cdb2 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,8 @@ List of all available pre-drawer attributes: * Sets GUIContent label for `SerializedProperty` to value from another field * `FieldLabelFromType` * Sets GUIContent label for `SerializedProperty` to value type from another field - +* `HideLabel` + * Sets GUIContent label for `SerializedProperty` to empty string ## Drawer diff --git a/Runtime/Predrawers/HideLabelAttribute.cs b/Runtime/Predrawers/HideLabelAttribute.cs new file mode 100644 index 0000000..d3130d9 --- /dev/null +++ b/Runtime/Predrawers/HideLabelAttribute.cs @@ -0,0 +1,14 @@ +using System; +using UnityEngine.Scripting; + +namespace Module.Inspector +{ + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] + public sealed class HideLabelAttribute : PredrawerModifierPropertyAttribute + { + [Preserve] + public HideLabelAttribute() + { + } + } +} \ No newline at end of file diff --git a/Runtime/Predrawers/HideLabelAttribute.cs.meta b/Runtime/Predrawers/HideLabelAttribute.cs.meta new file mode 100644 index 0000000..f66880a --- /dev/null +++ b/Runtime/Predrawers/HideLabelAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5f5c299642f040cabf74fd751c38e7ce +timeCreated: 1699115292 \ No newline at end of file diff --git a/Runtime/Validators/NotNullFieldAttribute.cs b/Runtime/Validators/NotNullFieldAttribute.cs index d27fb33..1eef1d4 100644 --- a/Runtime/Validators/NotNullFieldAttribute.cs +++ b/Runtime/Validators/NotNullFieldAttribute.cs @@ -6,9 +6,16 @@ namespace Module.Inspector [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] public class NotNullFieldAttribute : ValidatePropertyAttribute { + public readonly string errorMessage; + [Preserve] public NotNullFieldAttribute() { } + + public NotNullFieldAttribute(string errorMessage) + { + this.errorMessage = errorMessage; + } } } \ No newline at end of file diff --git a/package.json b/package.json index 4840a00..de244dd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.module.inspector", - "version": "1.8.2", + "version": "1.8.3", "displayName": "Module.Inspector", "description": "Custom inspector with various useful property drawers", "unity": "2019.2",