module-inspector/Editor/Predrawers/FieldLabelFromArrayIndexAttributeDrawer.cs

36 lines
No EOL
1.1 KiB
C#

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();
}
}
}