diff --git a/Editor/Predrawers/FieldLabelFromArrayIndexAttributeDrawer.cs b/Editor/Predrawers/FieldLabelFromArrayIndexAttributeDrawer.cs new file mode 100644 index 0000000..d9c00ad --- /dev/null +++ b/Editor/Predrawers/FieldLabelFromArrayIndexAttributeDrawer.cs @@ -0,0 +1,36 @@ +using System.Text; +using UnityEditor; +using UnityEngine; + +namespace Module.Inspector.Editor +{ + [CustomPropertyDrawer(typeof(FieldLabelFromArrayIndexAttribute))] + internal sealed class FieldLabelFroArrayIndexAttributeDrawer : PredrawerModifierPropertyDrawer + { + public override void Modify(PredrawerModifierPropertyAttribute attribute, SerializedProperty property, GUIContent label) + { + var att = (FieldLabelFromArrayIndexAttribute)attribute; + var spParent = property.GetParent(); + + if (spParent == null || !spParent.isArray) + return; + + var index = spParent.IndexOfProperty(property); + + if (index == -1) + return; + + var builder = new StringBuilder(); + + if (!string.IsNullOrEmpty(att.prefix)) + builder.Append(att.prefix); + + builder.Append(index); + + if (!string.IsNullOrEmpty(att.postfix)) + builder.Append(att.postfix); + + label.text = builder.ToString(); + } + } +} \ No newline at end of file diff --git a/Editor/Predrawers/FieldLabelFromArrayIndexAttributeDrawer.cs.meta b/Editor/Predrawers/FieldLabelFromArrayIndexAttributeDrawer.cs.meta new file mode 100644 index 0000000..71bf281 --- /dev/null +++ b/Editor/Predrawers/FieldLabelFromArrayIndexAttributeDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c3d86ca907fc4263af776ff820941320 +timeCreated: 1781374403 \ No newline at end of file diff --git a/README.md b/README.md index 5b0dd79..f7a60cd 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ List of all available pre-drawer attributes: * Sets GUIContent label for `SerializedProperty` to value type from another field * `FieldLabelFromToString` * Sets GUIContent label for `SerializedProperty` to values ToString-method +* `FieldLabelFromArrayIndex` + * Sets GUIContent label for `SerializedProperty` to array index with support for prefix, postfix, and array offset * `HideLabel` * Sets GUIContent label for `SerializedProperty` to empty string diff --git a/Runtime/Predrawers/FieldLabelFromArrayIndexAttribute.cs b/Runtime/Predrawers/FieldLabelFromArrayIndexAttribute.cs new file mode 100644 index 0000000..06ff701 --- /dev/null +++ b/Runtime/Predrawers/FieldLabelFromArrayIndexAttribute.cs @@ -0,0 +1,26 @@ +using System; + +namespace Module.Inspector +{ + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] + public sealed class FieldLabelFromArrayIndexAttribute : PredrawerModifierPropertyAttribute + { + public readonly string prefix; + public readonly string postfix; + public readonly int offset; + + public FieldLabelFromArrayIndexAttribute(string prefix, int offset = 0) + { + this.prefix = prefix; + postfix = null; + this.offset = offset; + } + + public FieldLabelFromArrayIndexAttribute(string prefix, string postfix, int offset = 0) + { + this.prefix = prefix; + this.postfix = postfix; + this.offset = offset; + } + } +} \ No newline at end of file diff --git a/Runtime/Predrawers/FieldLabelFromArrayIndexAttribute.cs.meta b/Runtime/Predrawers/FieldLabelFromArrayIndexAttribute.cs.meta new file mode 100644 index 0000000..83321fd --- /dev/null +++ b/Runtime/Predrawers/FieldLabelFromArrayIndexAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 84143cf4320e4ead85b143498a27f8c4 +timeCreated: 1781374352 \ No newline at end of file diff --git a/package.json b/package.json index 67733bb..cc3bca1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.module.inspector", - "version": "1.11.3", + "version": "1.12.0", "displayName": "Module.Inspector", "description": "Custom inspector with various useful property drawers", "unity": "2019.2",