Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

SerializedProperty.tooltip not working?

Discussion in 'Scripting' started by Desprez, Feb 1, 2017.

  1. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    I'm working on a custom editor, and I can't seem to retrieve the tooltip of a SerializedProperty.
    I'm getting the property itself just fine, but the tooltip is null.

    I can simply add the tooltip to the editor class, but I'd like to keep everything on the main script.
     
  2. Desprez

    Desprez

    Joined:
    Aug 31, 2012
    Posts:
    305
    After some digging, I see that this was a known issue by QA in 2015. Apparently it's still not fixed, :(
     
  3. Zielscheibe

    Zielscheibe

    Joined:
    May 7, 2014
    Posts:
    4
    Hi,
    I got the same problem and I came to the same conclusion after some research. It is a known bug which is ignored by Unity.

    If you just want to implement a custom editor, I may have a workaround for you. However, this does not work when implementing PropertyDrawers which I have to do...

    You can get the tooltip from the field instead from the serializedproperty. Let's assume you have a class MyClass and an editor MyClassEditor.


    Code (CSharp):
    1. public class MyClass : MonoBehaviour
    2. {
    3.     [Tooltip("My Tooltip")]
    4.     public float Foo;
    5.  
    6.     // Some Methods
    7. }
    8.  
    9. public class MyClassEditor : Editor
    10. {
    11.     public override void OnInspectorGUI()
    12.     {
    13.          MyClass instance = target as MyClass;
    14.          content = new GUIContent("Foo",
    15.              GetTooltip(instance.GetType().GetField("Foo"), true));
    16.     }
    17.  
    18.     public static string GetTooltip(FieldInfo field, bool inherit)
    19.     {
    20.         TooltipAttribute[] attributes
    21.              = field.GetCustomAttributes(typeof(TooltipAttribute), inherit)
    22.              as TooltipAttribute[];
    23.  
    24.          string ret = "";
    25.          if (attributes.Length > 0)
    26.              ret = attributes[0].tooltip;
    27.  
    28.          return ret;
    29.      }
    30. }
    31.  
    32.  

    Hope that helps :)
     
    Smithy43, AlexTuduran and biodam like this.
  4. AlexTuduran

    AlexTuduran

    Joined:
    Nov 26, 2016
    Posts:
    27
    @Zielscheibe You're a God-send. I've spent hours on getting the tooltip out of a property without success. Many thanks!

    [Edit]

    If someone works with serialized properties instead (and you should :)), you can use:

    Code (csharp):
    1. private static string GetSerializedPropertyTooltip<Type>(SerializedProperty serializedProperty, bool inherit) {
    2.     if (null == serializedProperty) {
    3.         return string.Empty;
    4.     }
    5.  
    6.     FieldInfo field = typeof(Type).GetField(serializedProperty.name);
    7.     if (null == field) {
    8.         return string.Empty;
    9.     }
    10.  
    11.     return GetFieldTooltip(field, inherit);
    12. }
    And use it like this:

    Code (csharp):
    1. [CustomEditor(typeof(MyComponent))]
    2. ...
    3. ...
    4. SerializedProperty myProperty = serializedObject.FindProperty("myProperty");
    5. ...
    6. ...
    7. string tooltip = GetSerializedPropertyTooltip<MyComponent>(myProperty, true);
    8. GUIContent tooltipContent = new GUIContent(myProperty.displayName, tooltip);
     
    Last edited: Oct 11, 2018
    Smithy43 likes this.
  5. Zielscheibe

    Zielscheibe

    Joined:
    May 7, 2014
    Posts:
    4
    Hi Alex,
    actually I got an even more interesting answer lately. You should check this out too. :)
    @nlebedenco found the following solution:

    Code (CSharp):
    1. label = EditorGUI.BeginProperty(position, label, property);
    So in the end is wasn't a bug. It was just bad documentation...
     
    Neohun, xodennisxo and AlexTuduran like this.
  6. AlexTuduran

    AlexTuduran

    Joined:
    Nov 26, 2016
    Posts:
    27
    I found that as well, but I don't know where to get a position from to draw to. I'm in a custom editor context and using the auto-layout versions of the methods (aka. using EditorGUILayout instead of EditorGUI), so your version suits me better at this point. Appreciated nevertheless.
     
  7. xodennisxo

    xodennisxo

    Joined:
    Mar 25, 2020
    Posts:
    16
    The disadvantage of "instance.GetType().GetField" is you must know what instance type the SerializedProperty is, however sometime it's not easy to know. For example, when you traverse a SerializedProperty:
    do{
    //what type the property belongs to now?
    }while (property.NextVisible(true));
    And BeginProperty you can use just with SerializedProperty. use whatever a placehoder Rect and null GUIcontent are ok:
    do{
    GUIContent temp = EditorGUI.BeginProperty(position, null, actionProperty);
    string tooltip = temp.tooltip;
    }while (property.NextVisible(true));