Lately I’ve been working on a project with several artists on a game, and they don’t always know what I meant by some of my variable names–even if I name it very specifically. I saw the new Property Attributes in Unity 4 (we just switched about a month ago) and I instantly thought about tooltips in inspector windows. So yesterday evening, I threw together two scripts that did just that. The first was a simple Tooltip script that inherited from PropertyAttribute, and then I got to work on the drawer class. After writing about 10 or so if statements, I got most of the inspector tooltips working.
There are a few, however, that I couldn’t get working. Namely enumerations, generics/arrays, and more advanced inspectors like gradient and Vector types. I know there is an EditorGUI.PropertyField
or something like that, but everytime I try to use this with say, a generic, Unity throws a Stack Overflow exception and the like and the entire inspector disappears. Could someone help me get these working? I’ll post the drawer class below, as well as the attribute class if anyone wants to use the existing properties in their project.
// Tooltip.cs - Does NOT go in Editor folder
using UnityEngine;
using System.Collections;
public class Tooltip : PropertyAttribute
{
public string EditorTooltip;
public Tooltip(string EditorTooltip)
{
this.EditorTooltip = EditorTooltip;
}
}
// This DOES go in the Editor folder (UnityEditor)
using System;
using System.ComponentModel;
using System.Diagnostics;
using UnityEditor;
using UnityEngine;
using Debug = System.Diagnostics.Debug;
[CustomPropertyDrawer(typeof(Tooltip))]
public class TooltipDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Tooltip tooltipAttribute = attribute as Tooltip;
if (property.propertyType == SerializedPropertyType.AnimationCurve)
{
property.animationCurveValue = EditorGUI.CurveField(position, new GUIContent(label.text, tooltipAttribute.EditorTooltip), property.animationCurveValue);
}
if (property.propertyType == SerializedPropertyType.Boolean)
{
property.boolValue = EditorGUI.Toggle(position, new GUIContent(label.text, tooltipAttribute.EditorTooltip), property.boolValue);
}
if (property.propertyType == SerializedPropertyType.Bounds)
{
property.boundsValue = EditorGUI.BoundsField(position, new GUIContent(label.text, tooltipAttribute.EditorTooltip), property.boundsValue);
}
if (property.propertyType == SerializedPropertyType.Color)
{
property.colorValue = EditorGUI.ColorField(position, new GUIContent(label.text, tooltipAttribute.EditorTooltip),
property.colorValue);
}
if (property.propertyType == SerializedPropertyType.Float)
{
property.floatValue = EditorGUI.FloatField(position,
new GUIContent(label.text, tooltipAttribute.EditorTooltip), property.floatValue);
}
if (property.propertyType == SerializedPropertyType.Integer)
{
property.intValue = EditorGUI.IntField(position, new GUIContent(label.text, tooltipAttribute.EditorTooltip), property.intValue);
}
if (property.propertyType == SerializedPropertyType.Rect)
{
property.rectValue = EditorGUI.RectField(position, new GUIContent(label.text, tooltipAttribute.EditorTooltip),
property.rectValue);
}
if (property.propertyType == SerializedPropertyType.String)
{
property.stringValue = EditorGUI.TextField(position,
new GUIContent(label.text, tooltipAttribute.EditorTooltip), property.stringValue);
}
}
}
To get the tooltips, you can create a “Test” script or something like that and add the attribute like this:
[Tooltip("Test string value")]
public bool TestValue = true;