I am writing a PropertyDrawer for a custom class that I have and I’m just drawing property fields with no labels. So all that gets drawn are an enum popup and a float slider.
I want to display a tooltip when you mouse over the enum popup that provides a description of the selected enum value, so I created a GUIContent with no label and a tooltip, but this didn’t work. It appears the tooltip in GUIContent only applies to the label.
Is it possible to display a tooltip when the user mouses over a property field that doesn’t have a label?
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(RequiredSkill))]
public class RequiredSkillDrawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
SerializedProperty skill = property.FindPropertyRelative("skill");
SerializedProperty level = property.FindPropertyRelative("level");
// Get the rectangles for the properties
Rect skillRect = new Rect(position.x, position.y, position.width/2-2, position.height);
Rect levelRect = new Rect(position.x + skillRect.width + 4, position.y, position.width/2-2, position.height);
// Get the tooltip for the currently selected skill
string tooltip = Skills.Description((Skills.Type)skill.enumValueIndex);
// Draw the properties
EditorGUI.PropertyField(skillRect, skill, new GUIContent("", tooltip));
EditorGUI.PropertyField(levelRect, level, GUIContent.none);
}
}
LOL Of course, I found a solution about 30 seconds after posting. I just created a LabelField right after my PropertyField that has the same Rect so that it takes up the same space on the screen, but has no text. So when you mouse over the EnumPopup, you’re really mousing over the LabelField that can’t be seens and that displays the tooltip. =P
// Draw the properties
EditorGUI.PropertyField(skillRect, skill, GUIContent.none);
EditorGUI.PropertyField(levelRect, level, GUIContent.none);
EditorGUI.LabelField(skillRect, new GUIContent("", tooltip)); // <-- Displays tooltip
Try getting a TooltipAttribute in your Drawer from this.fieldInfo.Attributes like so:
var tt = fieldInfo.GetCustomAttributes(typeof(TooltipAttribute), true).FirstOrDefault()
as TooltipAttribute;
if (tt != null) label.tooltip = tt.tooltip;
Yes, this works perfectly. Im using it like this: var tooltipAttribute = fieldInfo.GetAttribute<TooltipAttribute>(); label.tooltip = tooltipAttribute != null ? tooltipAttribute.tooltip : ""; Thanks!
If you found yourself here because you are trying to access the tooltip that already exists on a field, and are doing custom drawing in a custom editor window instead of a property drawer, you are able to access the tooltip string from the serialized property.
var tooltipString = mySerializedObject.FindProperty("myFieldName").tooltip;
Yes, this works perfectly. Im using it like this: var tooltipAttribute = fieldInfo.GetAttribute<TooltipAttribute>(); label.tooltip = tooltipAttribute != null ? tooltipAttribute.tooltip : ""; Thanks!
– Novack