Created
February 5, 2021 17:57
-
-
Save James-Frowen/1a62fdb8341a983031c0a5c1273fdd26 to your computer and use it in GitHub Desktop.
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 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