For convenience, I’ve created two PropertyDrawers to allow me to modify the display of some data in the Inspector without having to create a full-blown Editor. One property drawer allows me to “lock” a field, so that it won’t be edited, and the other allows me to display a HelpBox above the field.
The problem I’m encountering is that when I use both of these PropertyDrawer attributes on a single inspector variable, only one of them is actually applied. That isn’t the case with the built-in attributes, so I’m trying to determine what I’m doing wrong. Here is the code for the two property drawers:
Lockable Property Drawer:
[CustomPropertyDrawer(typeof(LockableAttribute))]
public class LockablePropertyDrawer : PropertyDrawer
{
// Whether the property is locked for editing. Always true at first.
private bool locked = true;
public override float GetPropertyHeight(SerializedProperty property,
GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI(Rect position,
SerializedProperty property,
GUIContent label)
{
// Locked toggle.
Rect lockedRect = new Rect(position.width, position.y, 20.0f, EditorGUIUtility.singleLineHeight);
locked = !EditorGUI.Toggle(lockedRect, !locked);
// If locked, disable GUI for the property.
if(locked) { GUI.enabled = false; }
// Draw the actual property.
position.width -= lockedRect.width;
EditorGUI.PropertyField(position, property, label, true);
// Re-enable GUI.
GUI.enabled = true;
}
}
HelpBox Property Drawer:
[CustomPropertyDrawer(typeof(HelpBoxAttribute))]
public class HelpBoxPropertyDrawer : PropertyDrawer
{
private const int helpBoxHeight = 20;
public override float GetPropertyHeight(SerializedProperty property,
GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, false) + helpBoxHeight;
}
public override void OnGUI(Rect position,
SerializedProperty property,
GUIContent label)
{
HelpBoxAttribute helpBoxAttribute = attribute as HelpBoxAttribute;
EditorGUI.HelpBox(position, helpBoxAttribute.message, MessageType.Info);
}
}
Using both at the same time:
[Lockable]
[HelpBox("This is a help message.")]
public string uniqueId = string.Empty;
An interesting thing is that whichever one is first will be drawn, while the other won’t be drawn. If I flip the order of the Lockable and Helpbox attributes on the variable, the Helpbox will draw, but not the lockable stuff. Adding log messages in the OnGUI functions shows that only one of the OnGUI functions is actually being called.