TooltipDrawer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2014 Google Inc. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #if UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2
  14. #define PRE_UNITY_4_3
  15. #endif
  16. using System;
  17. using System.Reflection;
  18. using UnityEngine;
  19. using UnityEditor;
  20. /*
  21. Custom Property Drawer to enable Tooltips for Inspector properties
  22. */
  23. [CustomPropertyDrawer(typeof(TooltipAttribute))]
  24. public class TooltipDrawer : PropertyDrawer
  25. {
  26. #if PRE_UNITY_4_3
  27. private GUIContent _newTooltipContent = null;
  28. private GUIContent previousTooltipContent = null;
  29. private Type fieldType = null;
  30. public override void OnGUI(Rect position, SerializedProperty property, GUIContent tooltipContent) {
  31. previousTooltipContent = tooltipContent;
  32. EditorGUI.BeginProperty(position, newTooltipContent, property);
  33. EditorGUI.BeginChangeCheck();
  34. switch(property.propertyType)
  35. {
  36. case SerializedPropertyType.Boolean:
  37. bool newBoolValue = EditorGUI.Toggle(position, newTooltipContent, property.boolValue);
  38. if(EditorGUI.EndChangeCheck()) property.boolValue = newBoolValue;
  39. break;
  40. case SerializedPropertyType.Enum:
  41. int newEnumValueIndex = (int)(object)EditorGUI.EnumPopup(position, newTooltipContent, Enum.Parse(GetFieldType(property), property.enumNames[property.enumValueIndex]) as Enum);
  42. if(EditorGUI.EndChangeCheck()) property.enumValueIndex = newEnumValueIndex;
  43. break;
  44. case SerializedPropertyType.Float:
  45. float newFloatValue = EditorGUI.FloatField(position, newTooltipContent, property.floatValue);
  46. if(EditorGUI.EndChangeCheck()) property.floatValue = newFloatValue;
  47. break;
  48. case SerializedPropertyType.Integer:
  49. int newIntValue = EditorGUI.IntField(position, newTooltipContent, property.intValue);
  50. if(EditorGUI.EndChangeCheck()) property.intValue = newIntValue;
  51. break;
  52. case SerializedPropertyType.String:
  53. string newStringValue = EditorGUI.TextField(position, newTooltipContent, property.stringValue);
  54. if(EditorGUI.EndChangeCheck()) property.stringValue = newStringValue;
  55. break;
  56. default:
  57. Debug.LogWarning("TooltipDrawer: Unsupported Type: " + property.propertyType);
  58. break;
  59. }
  60. }
  61. private GUIContent newTooltipContent {
  62. get {
  63. if(_newTooltipContent == null) {
  64. TooltipAttribute tooltipAttribute = attribute as TooltipAttribute;
  65. _newTooltipContent = new GUIContent(previousTooltipContent.text, tooltipAttribute.text);
  66. }
  67. return _newTooltipContent;
  68. }
  69. }
  70. private Type GetFieldType(SerializedProperty property) {
  71. if (fieldType == null) {
  72. Type parentClassType = property.serializedObject.targetObject.GetType();
  73. FieldInfo fieldInfo = parentClassType.GetField(property.name,
  74. BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
  75. if (fieldInfo == null) {
  76. Debug.LogWarning("TooltipDrawer: No field info found.");
  77. return null;
  78. }
  79. fieldType = fieldInfo.FieldType;
  80. }
  81. return fieldType;
  82. }
  83. #else
  84. public override void OnGUI(Rect position, SerializedProperty property, GUIContent tooltipContent) {
  85. var atr = (TooltipAttribute) attribute;
  86. var content = new GUIContent(tooltipContent.text, atr.text);
  87. EditorGUI.PropertyField(position, property, content);
  88. }
  89. #endif
  90. }