Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fix the display name when serializing property backing fields in Unity.
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
{
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(UsePropertyNameAttribute))]
public class UsePropertyNameAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var text = label.text;
var firstLetter = char.ToUpper(text[1]);
label.text = firstLetter + text.Substring(2, text.Length - 19);
EditorGUI.PropertyField(position, property, label);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.