Skip to content

Commit

Permalink
Fix for Tooltips for Unity 4.2
Browse files Browse the repository at this point in the history
Unity v4.3 fixed an issue where "Editor: PropertyDrawers can now call
EditorGUI.PropertyField to get the default implementation instead of
getting infinite recursion."
(http://unity3d.com/unity/whats-new/unity-4.3).
Added work around for previous versions that doesn’t use
EditorGUI.PropertyField

Fixes #3
  • Loading branch information
emmanuellemm committed Sep 15, 2014
1 parent 5e3581d commit d4f801e
Showing 1 changed file with 79 additions and 5 deletions.
84 changes: 79 additions & 5 deletions source/Plugins/Editor/TooltipDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,93 @@ You may obtain a copy of the License at
limitations under the License.
*/

#if UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2
#define PRE_UNITY_4_3
#endif

using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;


/*
Custom Property Drawer to enable Tooltips for Inspector properties
*/

[CustomPropertyDrawer(typeof(TooltipAttribute))]
public class TooltipDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
{

#if PRE_UNITY_4_3
private GUIContent _newTooltipContent = null;
private GUIContent previousTooltipContent = null;
private Type fieldType = null;

public override void OnGUI(Rect position, SerializedProperty property, GUIContent tooltipContent) {
previousTooltipContent = tooltipContent;
EditorGUI.BeginProperty(position, newTooltipContent, property);
EditorGUI.BeginChangeCheck();
switch(property.propertyType)
{
case SerializedPropertyType.Boolean:
bool newBoolValue = EditorGUI.Toggle(position, newTooltipContent, property.boolValue);
if(EditorGUI.EndChangeCheck()) property.boolValue = newBoolValue;
break;
case SerializedPropertyType.Enum:
int newEnumValueIndex = (int)(object)EditorGUI.EnumPopup(position, newTooltipContent, Enum.Parse(GetFieldType(property), property.enumNames[property.enumValueIndex]) as Enum);
if(EditorGUI.EndChangeCheck()) property.enumValueIndex = newEnumValueIndex;
break;
case SerializedPropertyType.Float:
float newFloatValue = EditorGUI.FloatField(position, newTooltipContent, property.floatValue);
if(EditorGUI.EndChangeCheck()) property.floatValue = newFloatValue;
break;
case SerializedPropertyType.Integer:
int newIntValue = EditorGUI.IntField(position, newTooltipContent, property.intValue);
if(EditorGUI.EndChangeCheck()) property.intValue = newIntValue;
break;
case SerializedPropertyType.String:
string newStringValue = EditorGUI.TextField(position, newTooltipContent, property.stringValue);
if(EditorGUI.EndChangeCheck()) property.stringValue = newStringValue;
break;
default:
Debug.LogWarning("TooltipDrawer: Unsupported Type: " + property.propertyType);
break;
}
}

private GUIContent newTooltipContent {
get {
if(_newTooltipContent == null) {
TooltipAttribute tooltipAttribute = attribute as TooltipAttribute;
_newTooltipContent = new GUIContent(previousTooltipContent.text, tooltipAttribute.text);
}
return _newTooltipContent;
}
}

private Type GetFieldType(SerializedProperty property) {
if (fieldType == null) {
Type parentClassType = property.serializedObject.targetObject.GetType();
FieldInfo fieldInfo = parentClassType.GetField(property.name,
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);

if (fieldInfo == null) {
Debug.LogWarning("TooltipDrawer: No field info found.");
return null;
}
fieldType = fieldInfo.FieldType;
}
return fieldType;
}

#else

public override void OnGUI(Rect position, SerializedProperty property, GUIContent tooltipContent) {
var atr = (TooltipAttribute) attribute;
var content = new GUIContent(label.text, atr.text);
EditorGUI.PropertyField(position, prop, content);
var content = new GUIContent(tooltipContent.text, atr.text);
EditorGUI.PropertyField(position, property, content);
}
}

#endif
}

0 comments on commit d4f801e

Please sign in to comment.