Skip to content

Instantly share code, notes, and snippets.

@atori708
Created November 26, 2022 09:10
Show Gist options
  • Save atori708/9c60e8462db41ab29bb815095437cc40 to your computer and use it in GitHub Desktop.
Save atori708/9c60e8462db41ab29bb815095437cc40 to your computer and use it in GitHub Desktop.
【Unity】ラベルを型名で表示するためのPropertyAttribute
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
/// <summary>
/// ラベルで型名を表示するための<see cref="PropertyAttribute"/>
/// </summary>
public class LabelTypeNameAttribute : PropertyAttribute
{
}
#if UNITY_EDITOR
/// <summary>
/// <see cref="LabelTypeNameAttribute"/>用の<see cref="PropertyDrawer"/>
/// </summary>
[CustomPropertyDrawer(typeof(LabelTypeNameAttribute))]
public class LabelTypeNameAttributeDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var labelTitle = GetTypeName(property);
if(!string.IsNullOrEmpty(labelTitle)) {
label.text = GetTypeName(property);
}
EditorGUI.PropertyField(position, property, label, true);
}
string GetTypeName(SerializedProperty serializedProperty)
{
string ret = "";
switch (serializedProperty.propertyType) {
case SerializedPropertyType.ManagedReference:
ret = serializedProperty.managedReferenceValue.GetType().Name;
break;
}
return ret;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment