PropertyTooltipDrawer.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // This DOES go in the Editor folder (UnityEditor)
  2. using System;
  3. using UnityEditor;
  4. using UnityEngine;
  5. [CustomPropertyDrawer(typeof(FriendlyName))]
  6. public class PropertyTooltipDrawer : PropertyDrawer
  7. {
  8. private string GetDisplay(SerializedProperty property, FriendlyName attr, GUIContent label )
  9. {
  10. if( attr.friendlyName == null || attr.friendlyName.Length <= 0 )
  11. {
  12. return label.text;
  13. }
  14. return attr.friendlyName;
  15. }
  16. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  17. {
  18. FriendlyName friendlyNameAttr = attribute as FriendlyName;
  19. if (property.propertyType == SerializedPropertyType.AnimationCurve)
  20. {
  21. property.animationCurveValue = EditorGUI.CurveField(position, new GUIContent(GetDisplay(property, friendlyNameAttr, label)), property.animationCurveValue);
  22. }
  23. else if (property.propertyType == SerializedPropertyType.Boolean)
  24. {
  25. property.boolValue = EditorGUI.Toggle(position, new GUIContent(GetDisplay(property, friendlyNameAttr, label)), property.boolValue);
  26. }
  27. else if (property.propertyType == SerializedPropertyType.Bounds)
  28. {
  29. property.boundsValue = EditorGUI.BoundsField(position, new GUIContent(GetDisplay(property, friendlyNameAttr, label)), property.boundsValue);
  30. }
  31. else if (property.propertyType == SerializedPropertyType.Color)
  32. {
  33. property.colorValue = EditorGUI.ColorField(position, new GUIContent(GetDisplay(property, friendlyNameAttr, label)),
  34. property.colorValue);
  35. }
  36. else if (property.propertyType == SerializedPropertyType.Float)
  37. {
  38. property.floatValue = EditorGUI.FloatField(position,
  39. new GUIContent(GetDisplay(property, friendlyNameAttr, label)), property.floatValue);
  40. }
  41. else if (property.propertyType == SerializedPropertyType.Integer)
  42. {
  43. property.intValue = EditorGUI.IntField(position, new GUIContent(GetDisplay(property, friendlyNameAttr, label)), property.intValue);
  44. }
  45. else if (property.propertyType == SerializedPropertyType.Rect)
  46. {
  47. property.rectValue = EditorGUI.RectField(position, new GUIContent(GetDisplay(property, friendlyNameAttr, label)),
  48. property.rectValue);
  49. }
  50. else if (property.propertyType == SerializedPropertyType.String)
  51. {
  52. property.stringValue = EditorGUI.TextField(position,
  53. new GUIContent(GetDisplay(property, friendlyNameAttr, label)), property.stringValue);
  54. }
  55. else if( property.propertyType == SerializedPropertyType.Vector2 )
  56. {
  57. property.vector2Value = EditorGUI.Vector2Field(position,
  58. new GUIContent(GetDisplay(property, friendlyNameAttr, label)), property.vector2Value);
  59. }
  60. else if (property.propertyType == SerializedPropertyType.Vector3)
  61. {
  62. property.vector3Value = EditorGUI.Vector3Field(position,
  63. new GUIContent(GetDisplay(property, friendlyNameAttr, label)), property.vector3Value);
  64. }
  65. else if (property.propertyType == SerializedPropertyType.Vector4)
  66. {
  67. property.vector4Value = EditorGUI.Vector4Field(position,
  68. GetDisplay(property, friendlyNameAttr, label), property.vector4Value);
  69. }
  70. }
  71. }