Search Unity

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

Discussion in 'Immediate Mode GUI (IMGUI)' started by S_Darkwell, Mar 19, 2015.

  1. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    320
    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#):

    Code (CSharp):
    1. using UnityEngine;
    2. public class Example : MonoBehaviour
    3. {
    4.    [Tooltip("Here is a tooltip")]
    5.    public float testFloat;
    6. }
    The inspector (C#):

    Code (CSharp):
    1. using UnityEditor;
    2. [CustomEditor(typeof(Example))]
    3. public class ExampleEditor : Editor
    4. {
    5.    private void OnEnable()
    6.    {
    7.       Target = (Example) target;
    8.    }
    9.  
    10.    public override void OnInspectorGUI()
    11.    {
    12.       Target.testFloat = EditorGUILayout.FloatField(newGUIContent("Test Float","Here is a tooltip"),Target.testFloat);
    13.    }
    14. }

    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!
     
  2. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    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.

    Code (CSharp):
    1.     [Tooltip("This is a tool tip")]
    2.     public bool myAwesomeBool = false;
    3.  
    4.     public void DoStuff()
    5.     {
    6.       TooltipAttribute toolTip = GetTooltip(this.GetType().GetField("myAwesomeBool"), true);
    7.     }
    8.  
    9.  
    10.     private TooltipAttribute GetTooltip(FieldInfo field, bool inherit)
    11.     {
    12.       TooltipAttribute[] attributes = field.GetCustomAttributes(typeof(TooltipAttribute), inherit) as TooltipAttribute[];
    13.  
    14.       return attributes.Length > 0 ? attributes[0] : null;
    15.     }
    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.
     
    S_Darkwell likes this.
  3. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    320
    @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!
     
    BMayne likes this.
  4. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    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

    Code (CSharp):
    1. [CustomEditor(typeof(Example))]
    2. public class ExampleEditor : Editor
    3. {
    4.   //We create our property
    5.   SerializedProperty testFloat = null;
    6.  
    7.   private void OnEnable()
    8.   {
    9.     //Assign it on enable
    10.     testFloat = serializedObject.FindProperty("testFloat");
    11.   }
    12.  
    13.   public override void OnInspectorGUI()
    14.   {
    15.     // The really cool this about this is the fact that you don't have to say what type it is.
    16.     // It will draw the correct editor. If you have any property drawers (or attributes which drawers target)
    17.     // this will draw them instead. This includes the ToolTipAttribute.
    18.     EditorGUILayout.PropertyField(testFloat);
    19.  
    20.     /* Other Notes
    21.      *  You can now multi edit objects ( you can't do this when you use Target )
    22.      *  You can now Undo ( you can't do this as well as you can with serialized properties )
    23.      */
    24.   }
    25. }
     
    kendallroth and vedram like this.
  5. S_Darkwell

    S_Darkwell

    Joined:
    Oct 20, 2013
    Posts:
    320
    @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!
     
    BMayne likes this.
  6. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Stumbled across this looking for the same thing, and I believe you need to add:
    Code (CSharp):
    1. public override void OnInspectorGUI() {
    2.     serializedObject.Update();
    3.  
    4.     ...
    5.  
    6.     serializedObject.ApplyModifiedProperties();
    7. }
    To OnInspectorGUI, as my fields weren't actually saving.
     
    VLukianenko likes this.