1.10.0: Added FieldLabelFromEnum for elements in an array

This commit is contained in:
Anders Ejlersen 2024-11-21 19:30:29 +01:00
parent ea527c9b33
commit a804633a14
6 changed files with 74 additions and 1 deletions

View file

@ -0,0 +1,42 @@
using System;
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(FieldLabelFromEnumAttribute))]
internal sealed class FieldLabelFromEnumAttributeDrawer : PredrawerModifierPropertyDrawer
{
public override void Modify(PredrawerModifierPropertyAttribute attribute, SerializedProperty property, GUIContent label)
{
var att = (FieldLabelFromEnumAttribute)attribute;
if (!att.type.IsEnum)
return;
SerializedProperty sp = property.GetParent();
if (sp == null)
return;
var index = sp.IndexOfProperty(property);
if (index == -1)
return;
try
{
string str = Enum.GetName(att.type, index);
if (att.nicify)
str = ObjectNames.NicifyVariableName(str);
label.text = str;
}
catch (Exception)
{
// Ignore
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: af2e8cbad9e548688949c9dc4d1a127e
timeCreated: 1732212390

View file

@ -64,6 +64,8 @@ List of all available pre-drawer attributes:
* `FieldLabelFrom` * `FieldLabelFrom`
* Sets GUIContent label for `SerializedProperty` to value from another field * Sets GUIContent label for `SerializedProperty` to value from another field
* `FieldLabelFromEnum`
* Sets GUIContent label for `SerializedProperty` to enum name value of array index in parent 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` * `HideLabel`

View file

@ -0,0 +1,23 @@
using System;
namespace Module.Inspector
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public sealed class FieldLabelFromEnumAttribute : PredrawerModifierPropertyAttribute
{
public readonly Type type;
public readonly bool nicify;
public FieldLabelFromEnumAttribute(Type type)
{
this.type = type;
nicify = true;
}
public FieldLabelFromEnumAttribute(Type type, bool nicify)
{
this.type = type;
this.nicify = nicify;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: be6a15d8da91402c9a038f8a08fc4772
timeCreated: 1732212352

View file

@ -1,6 +1,6 @@
{ {
"name": "com.module.inspector", "name": "com.module.inspector",
"version": "1.9.5", "version": "1.10.0",
"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",