Retrieve Variable's [Tooltip()] Property From a Custom Inspector (C#)

Hello all!

I have been searching for a solution to this question for quite a while, unfortunately, to no avail:

How does one retrieve a script’s [[Tooltip()] property from within a Custom Inspector?

I wish to retrieve the value of a tooltip defined within a script using Unity 4.6+'s [Tooltip()] attribute. In its place, I am writing what you see below, which seems redundant:

The script (C#):

using UnityEngine;
public class Example : MonoBehaviour
{
   [Tooltip("Here is a tooltip")]
   public float testFloat;
}

The inspector (C#):

using UnityEditor;
[CustomEditor(typeof(Example))]
public class ExampleEditor : Editor
{
   private void OnEnable()
   {
      Target = (Example) target;
   }

   public override void OnInspectorGUI()
   {
      Target.testFloat = EditorGUILayout.FloatField(newGUIContent("Test Float","Here is a tooltip"),Target.testFloat);
   }
}

Replacing “Here is a tooltip” in the custom inspector with a call to the field’s [Tooltip()] property will not only save time and code, but will make the script far more maintainable.

Thank you greatly in advance!

Hey there,

What you are looking for has nothing to do with Unity directly but is a C# feature. If you would like to capture Attributes you use the class type.

    [Tooltip("This is a tool tip")]
    public bool myAwesomeBool = false;

    public void DoStuff()
    {
      TooltipAttribute toolTip = GetTooltip(this.GetType().GetField("myAwesomeBool"), true);
    }


    private TooltipAttribute GetTooltip(FieldInfo field, bool inherit)
    {
      TooltipAttribute[] attributes = field.GetCustomAttributes(typeof(TooltipAttribute), inherit) as TooltipAttribute[];

      return attributes.Length > 0 ? attributes[0] : null;
    }

Just as a heads up are you just trying to keep the tool tip if the user writes one? If so there is a much much easier way to do that and it requires a heck of a lot less of code.

1 Like

@BMayne :

Thank you so much for that code!

Yes. I’m creating my own editor functions where I hope to retain the tooltips defined within scripts themselves. I would absolutely be interested in the “heck of a lot less code” way!

1 Like

Okay, have you ever heard of Serialized Properties?

These are what Unity uses behind the scenes for most of their editor stuff. Here is your class redone with Serialized Properties

[CustomEditor(typeof(Example))]
public class ExampleEditor : Editor
{
  //We create our property
  SerializedProperty testFloat = null;

  private void OnEnable()
  {
    //Assign it on enable
    testFloat = serializedObject.FindProperty("testFloat");
  }

  public override void OnInspectorGUI()
  {
    // The really cool this about this is the fact that you don't have to say what type it is.
    // It will draw the correct editor. If you have any property drawers (or attributes which drawers target)
    // this will draw them instead. This includes the ToolTipAttribute.
    EditorGUILayout.PropertyField(testFloat);

    /* Other Notes
     *  You can now multi edit objects ( you can't do this when you use Target )
     *  You can now Undo ( you can't do this as well as you can with serialized properties )
     */
  }
}
2 Likes

@BMayne :

Wow! That’s incredibly helpful. Thank you so much!

Indeed, I had actually looked into SerializedProperty quite a bit, but I had watched an in-depth editor tutorial that I had (mistakenly) understood to be stating that serializedObject and SerializedProperty could only apply to ScriptableObject’s.

The realization that this is incorrect, and that SerializedProperty automatically applies attributes (which I never knew) will greatly shorten my custom inspector scripts and save me a vast amount of effort, to say the least.

Thank you so much, my friend!

1 Like

Stumbled across this looking for the same thing, and I believe you need to add:

public override void OnInspectorGUI() {
    serializedObject.Update();

    ...

    serializedObject.ApplyModifiedProperties();
}

To OnInspectorGUI, as my fields weren’t actually saving.

1 Like