Created
November 26, 2022 09:10
-
-
Save atori708/9c60e8462db41ab29bb815095437cc40 to your computer and use it in GitHub Desktop.
【Unity】ラベルを型名で表示するための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; | |
#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