1.8.3: Added HideLabel attribute

This commit is contained in:
Anders Ejlersen 2023-11-04 17:47:58 +01:00
parent 61c7356b3a
commit 3259b62387
12 changed files with 97 additions and 8 deletions

View file

@ -35,7 +35,7 @@ namespace Module.Inspector.Editor
if (value.drawer.Validate(value.attribute, property)) if (value.drawer.Validate(value.attribute, property))
continue; continue;
validationError += value.drawer.GetValidationError(property); validationError += value.drawer.GetValidationError(value.attribute, property);
isValid = false; isValid = false;
} }
@ -47,8 +47,8 @@ namespace Module.Inspector.Editor
label.tooltip = validationError; label.tooltip = validationError;
else else
label.tooltip += "\n" + validationError; label.tooltip += "\n" + validationError;
GUI.color = Color.red; EditorIcons.SetErrorIcon(label);
} }
if (result.draw != null) if (result.draw != null)

View file

@ -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;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: df07b497331149cfb8ce6e544247cb9f
timeCreated: 1699115325

View file

@ -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;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 07b74dac541c43cb8a89bd0a1b577ec1
timeCreated: 1699115532

View file

@ -5,6 +5,6 @@ namespace Module.Inspector.Editor
public abstract class ValidatePropertyDrawer : AbstractPropertyDrawer public abstract class ValidatePropertyDrawer : AbstractPropertyDrawer
{ {
public abstract bool Validate(ValidatePropertyAttribute attribute, SerializedProperty property); public abstract bool Validate(ValidatePropertyAttribute attribute, SerializedProperty property);
public abstract string GetValidationError(SerializedProperty property); public abstract string GetValidationError(ValidatePropertyAttribute attribute, SerializedProperty property);
} }
} }

View file

@ -42,9 +42,14 @@ namespace Module.Inspector.Editor
return true; 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;
} }
} }
} }

View file

@ -66,7 +66,8 @@ List of all available pre-drawer attributes:
* Sets GUIContent label for `SerializedProperty` to value from another field * Sets GUIContent label for `SerializedProperty` to value from another field
* `FieldLabelFromType` * `FieldLabelFromType`
* Sets GUIContent label for `SerializedProperty` to value type from another field * Sets GUIContent label for `SerializedProperty` to value type from another field
* `HideLabel`
* Sets GUIContent label for `SerializedProperty` to empty string
## Drawer ## Drawer

View file

@ -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()
{
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5f5c299642f040cabf74fd751c38e7ce
timeCreated: 1699115292

View file

@ -6,9 +6,16 @@ namespace Module.Inspector
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public class NotNullFieldAttribute : ValidatePropertyAttribute public class NotNullFieldAttribute : ValidatePropertyAttribute
{ {
public readonly string errorMessage;
[Preserve] [Preserve]
public NotNullFieldAttribute() public NotNullFieldAttribute()
{ {
} }
public NotNullFieldAttribute(string errorMessage)
{
this.errorMessage = errorMessage;
}
} }
} }

View file

@ -1,6 +1,6 @@
{ {
"name": "com.module.inspector", "name": "com.module.inspector",
"version": "1.8.2", "version": "1.8.3",
"displayName": "Module.Inspector", "displayName": "Module.Inspector",
"description": "Custom inspector with various useful property drawers", "description": "Custom inspector with various useful property drawers",
"unity": "2019.2", "unity": "2019.2",