Last active
February 10, 2025 08:44
-
-
Save FlaShG/49fcaf93bd8682a9722fd11950345b90 to your computer and use it in GitHub Desktop.
Fix the display name when serializing property backing fields in Unity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// <summary> | |
/// Use this attribute in combination with a [SerializeField] attribute on top of a property to display the property name. Example: | |
/// [field: SerializeField, UsePropertyName] | |
/// public int number { get; private set; } | |
/// </summary> | |
public class UsePropertyNameAttribute : PropertyAttribute | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
[CustomPropertyDrawer(typeof(UsePropertyNameAttribute))] | |
public class UsePropertyNameAttributeDrawer : PropertyDrawer | |
{ | |
// TODO Make useful for array properties | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
if (property.name.EndsWith("k__BackingField")) | |
{ | |
FixLabel(label); | |
} | |
EditorGUI.PropertyField(position, property, label, true); | |
} | |
private static void FixLabel(GUIContent label) | |
{ | |
var text = label.text; | |
var firstLetter = char.ToUpper(text[1]); | |
label.text = firstLetter + text.Substring(2, text.Length - 19); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment