- Home /
How can I show tooltip on property field?
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);
}
}
Answer by Moohasha · Dec 26, 2014 at 04:21 PM
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
Answer by Ross_B · Oct 06, 2016 at 01:27 PM
You can use a normal Tooltip on the serialized field and the property field will render it on hover.
In the object being inspected:
[Tooltip("My tooltip")]
[SerializeField] protected Transform transform;
In custom inspector:
SerializedProperty serTransform = serializedObject.FindProperty("transform");
//...
serializedObject.Update();
EditorGUILayout.PropertyField(serTransform);
serializedObject.ApplyModifiedProperties();
Answer by turbanov · Oct 16, 2017 at 11:30 AM
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!
Answer by cmersereau · Apr 29 at 06:52 PM
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;
Your answer

Follow this Question
Related Questions
How can I correctly draw properties instead of fields in a custom property drawer? 0 Answers
How to get the target object value from a PropertyDrawer for an array of objects? 1 Answer
Using DuplicateCommand and DeleteCommand with Properties 0 Answers
Caching data for a PropertyDrawer 1 Answer
Accessing properties with spaces? 1 Answer