Skip to content

Instantly share code, notes, and snippets.

@James-Frowen
Created February 5, 2021 17:57
Show Gist options
  • Save James-Frowen/1a62fdb8341a983031c0a5c1273fdd26 to your computer and use it in GitHub Desktop.
Save James-Frowen/1a62fdb8341a983031c0a5c1273fdd26 to your computer and use it in GitHub Desktop.
using System;
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
#endif
namespace JamesFrowen.PositionSync
{
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class BackingFieldNameAttribute : PropertyAttribute
{
public string ReplacementName { get; set; }
}
[CustomPropertyDrawer(typeof(BackingFieldNameAttribute), true)]
public class BackingFieldNameDrawer : PropertyDrawer
{
const string backingPrefix = "<";
const string backingSufix = ">k__Backing Field";
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
var attr = this.attribute as BackingFieldNameAttribute;
if (!string.IsNullOrEmpty(attr.ReplacementName))
{
label.text = attr.ReplacementName;
}
else
{
var text = label.text;
var replaced = text.Replace(backingPrefix, "").Replace(backingSufix, "");
label.text = replaced;
}
EditorGUI.PropertyField(position, property, label, true);
EditorGUI.EndProperty();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment