Added FieldLabelFrom attribute

This commit is contained in:
Anders Ejlersen 2021-12-04 12:36:50 +01:00
parent 52ee51c61c
commit 81bac32538
6 changed files with 60 additions and 0 deletions

3
Editor/Predrawers.meta Normal file
View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b11bc545d74c48e4aaac5cd734c534a9
timeCreated: 1638612721

View file

@ -0,0 +1,25 @@
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(FieldLabelFromAttribute))]
internal sealed class FieldLabelFromAttributeDrawer : PredrawerModifierPropertyDrawer
{
public override void Modify(PredrawerModifierPropertyAttribute attribute, SerializedProperty property, GUIContent label)
{
var att = (FieldLabelFromAttribute)attribute;
SerializedProperty sp = property.GetRelativeProperty(att.fieldName);
if (sp == null)
return;
string str = sp.GetValueAsString();
if (att.nicify)
str = ObjectNames.NicifyVariableName(str);
label.text = str;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 30eda277627d4bca8ddce716ef21b657
timeCreated: 1638612733

3
Runtime/Predrawers.meta Normal file
View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 968b630d4caa4605886ae1c1d7f3bdd8
timeCreated: 1638612567

View file

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

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b3ebd4bca514492f87bc5660d98a1a6c
timeCreated: 1638612604