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.

SerializedProperty is empty with [Tooltip] attribute

Discussion in 'Editor & General Support' started by Vanamerax, Dec 29, 2014.

  1. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    934
    Maybe Im missing something here, but Im having problems regarding tooltips:

    I have a class with several properties, which each have a [Tooltip("")] attribute assigned to them. However, when I get the SerializedProperty's .tooltip member, it is empty. Is this a bug or am I doing something wrong? Running Unity 4.6.1f1 here

    here is my code:
    Code (CSharp):
    1. private void DrawPropertyField(Rect position, SerializedProperty property, GUIContent label)
    2. {
    3.    GUIContent label = new GUIContent(property.name, property.tooltip);
    4.    Rect propertyRect = position;
    5.    propertyRect.width -= labelWidth;
    6.    propertyRect.x += labelWidth;
    7.    EditorGUI.PrefixLabel(position, label);
    8.    EditorGUI.PropertyField(propertyRect, property, GUIContent.none);
    9. }
    Also, for people who are wondering why Im not just using PropertyField, the labels are eating away too much space for my desire, so I am overwriting it this way. If there is another better way please let me know.
     
  2. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    934
    Anyone has got any idea?
     
  3. RElam

    RElam

    Joined:
    Nov 16, 2009
    Posts:
    375
    Nope, same problem here, seems broken. This and other bugs/flaws have made my initial foray into PropertyDrawers very disappointing.
     
  4. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    Seems to be still broken in 5.2 =(

    My repro case:

    "My" custom class:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. [Serializable]
    5. public struct Test
    6. {
    7.     [SerializeField]
    8.     private int intValue;
    9. }
    10.  
    MonoBehaviour with field of the Test type:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Tooltips : MonoBehaviour
    4. {
    5.     [Tooltip("I'm invisible tooltip :(")]
    6.     public Test test;
    7.  
    8.     [Tooltip("I'm visible tooltip :)")]
    9.     public Vector3 vector;
    10. }
    11.  
    The drawer itself:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomPropertyDrawer(typeof(Test))]
    5. public class TestDrawer : PropertyDrawer
    6. {
    7.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    8.     {
    9.         SerializedProperty intValue = property.FindPropertyRelative("intValue");
    10.         EditorGUI.PropertyField(position, intValue, label);
    11.     }
    12. }
    Both property.tooltip and label.tooltip are empty for me.

    Sent a bug report (case 728880).

    Or I missing something here too? o_O
     
    Last edited: Sep 19, 2015
  5. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    Good news (hopefully) - QA confirmed it's a bug and it was reproduced on the 5.1, 5.2 and 5.3 Unity versions!
     
  6. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    934
    Thanks for letting us know! I hope we will see a patch fix for this soon
     
  7. Novack

    Novack

    Joined:
    Oct 28, 2009
    Posts:
    824
    3 years later, still the bug present.

    Simple workaround to restore the original tooltip to the label: on your PropertyDrawer OnGUI method, add these lines:
    Code (CSharp):
    1. var tooltipAttribute = fieldInfo.GetAttribute<TooltipAttribute>();
    2. label.tooltip = tooltipAttribute != null ? tooltipAttribute.tooltip : "";
    Found the right answer here.

    Looks like the original issues is simply marked as "won fix", without any detail.
    https://issuetracker.unity3d.com/is...eld-of-the-serializedproperty-is-always-empty
     
    Last edited: Jul 5, 2018
    codestage likes this.
  8. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    Sad to hear that! My case where I reported it is having state Closed (case 728880).

    It's worth trying submitting a new bug report, perhaps there is a regression or fix didn't get into the main trunk.
     
  9. TCROC

    TCROC

    Joined:
    Aug 15, 2015
    Posts:
    149
    I can confirm that this bug is still here :(. I will be using the workaround for now :)
     
  10. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    karl_jones, Novack and Vanamerax like this.
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    I fixed it the other day :D We had a bug report about our Localization property drawers not displaying tooltips, after investigation I discovered it was an issue for all PropertyDrawers. The fix was actually really simple, the tooltip data is in the Property handler code, we just didn't pass it through. The original bug was closed as low priority in 2017.
    Moral of the story, keep making bug reports, it brings the old issues back into light so we can revaluate them.
     
  12. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    Thank you Karl! While you're at the tooltip, could you please remove the restriction from the TooltipAttribute that it can be used on fields only? This should be a trivial change without any risk I assume.

    I would like to use the built-in TooltipAttribute to display them as "class comments", see here.
     
  13. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    934
    Took some time and fix was similar to what I had expected, but at least I am glad that this has been fixed, thank you!
     
    karl_jones likes this.
  14. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    Ill bring it up with the Editor team but this is more of a feature request than a bug fix so its harder to justify.
     
    phobos2077 and Peter77 like this.
  15. Novack

    Novack

    Joined:
    Oct 28, 2009
    Posts:
    824
    Thanks Karl! Would be great if "low priority" wasnt a valid reason for closing an issue. Could that be reviewed? Takes time and dedication to open an issue, is a pitty to have them closed merely for bureaucratic reasons.
     
    Last edited: Mar 19, 2021
    phobos2077 and karl_jones like this.
  16. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    Yeah I'm not really sure about that justification but it was 4 years ago so a lot has changed in our bug handling since then.
     
    Novack likes this.
  17. Michael-Ryan

    Michael-Ryan

    Joined:
    Apr 10, 2009
    Posts:
    184
    Karl, do you now what releases are expected to include your fix? My current project is still using Unity 2018.4.30. Is your fix expected to be included in 2018 LTS?
     
  18. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    I only requested as far as 2019.4 as 2018.4 only really accepts high priority bugs now. I'll create a 2018.4 request and see if it gets accepted.
     
    codestage likes this.
  19. BarbaraTheGoodEvil

    BarbaraTheGoodEvil

    Joined:
    Mar 2, 2018
    Posts:
    4
    Is this fixed in 2020.3 as well? I assumed as much because it's fixed for 2020.2.X but my Custom Inspector still get's empty strings for properties that have a tooltip
     
  20. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    codestage likes this.
  21. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    287
    Any reason why this hasn't made it into the 2020.3 LTS, but is in 2020.2?
     
    leozzyzheng2 likes this.
  22. stevendimmerse

    stevendimmerse

    Joined:
    Aug 7, 2018
    Posts:
    9
    Is it intended that in OnInspectorGUI when you do serializedObject.FindProperty the SerializedProperty.tooltip returns empty, It seems like a bug that there is a tooltip field there yet it always returns and empty string.
     
  23. YondernautsGames

    YondernautsGames

    Joined:
    Nov 24, 2014
    Posts:
    287
    What Unity version? It has been fixed fairly recently, so the fix has only really made it into the LTS versions from the last couple of months. It's now working for me in the latest 2018.4 / 2019.4
     
  24. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    codestage and YondernautsGames like this.
  25. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
  26. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    codestage and Peter77 like this.
  27. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    Wow, thank you. I really just need the restriction on the TooltipAttribute to be removed, so that I can add the tooltip to a class rather than fields only.
     
    karl_jones likes this.
  28. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    This should be available in 2021.2 :)
     
    Peter77 and codestage like this.
  29. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    That's excellent, thank you very much!
     
    karl_jones likes this.
  30. PigletPants

    PigletPants

    Joined:
    Sep 19, 2019
    Posts:
    23
    This is still broken in 2020.3.14f1
     
  31. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    Please file a bug report. This landed in 2020.2.5f1 so if its not working in 2020.3.14f1 then it could be a different bug.
     
  32. PigletPants

    PigletPants

    Joined:
    Sep 19, 2019
    Posts:
    23
    codestage and karl_jones like this.
  33. PigletPants

    PigletPants

    Joined:
    Sep 19, 2019
    Posts:
    23
    Just updating to let anyone reading this know that the unity dev team discussed this and determined that this behaviour is by design. The resolution note:

    Using the [Tooltip] doesn't assign to SerializedProperty.tooltip. Hence why the .tooltip field is empty. The PropertyDrawing system uses the attribute tooltip value directly when rendering. So the current behavior is expected.
     
  34. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    This basically does means we can't draw properties with anything except
    EditorGUI \ EditorGUILayout.PropertyField if we would like to customize field behavior and keep tooltip which was set using TooltipAttribute..

    How this can be by design?..

    @karl_jones
     
  35. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    I didn't look into this but it looks like the tooltip attribute is not part of the serializedobject, we instead look it up when rendering however if you are calling into an EditorGUI method then we don't have access to that info and just the serialized property. I guess we would need to extract the attribute and put it into the serialized property but that may be harder than it sounds as we are just dealing with serialized paths at this point and not classes/instances. Ill try and take a closer look in a couple of weeks (after the 14th) when I have some free time to see what can be done.
     
    Peter77 and codestage like this.
  36. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    Hey took a look at this. So at the moment resolving the tooltip in EditorGUI.FloatField(guicontent, float) etc would be impossible because we have no idea what the property is inside of FloatField. This is because we lookup the attribute using the SerializedProperty which we dont have access to in FloatField. So Instead of trying to resolve it here I took a look into the serializedproperty tooltip property and modified the code to do the attribute lookup here instead, which makes more sense to me. So its now working. Ill push the changes for review and if things still work and dont explode then this should be a simple change :)
     
    PigletPants, codestage and Peter77 like this.
  37. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    That's awesome news, thank you for fixing it or us, @karl_jones ! Looking forward to see it released ^.^
     
    karl_jones likes this.
  38. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    Hey this is now fixed in 2022.1.0a12
    I have also landed a backport in 2020.3.22f1, 2021.2.3f1 and 2019.4.33f1.
     
    Last edited: Nov 11, 2021
    Novack and codestage like this.
  39. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    Woohoo!!
    I've just checked, and it works at 2022.1.0a12, this is just unbelievable, waited for it for the 6 years, thank you so much Karl, you made my day!
     
    Novack, Peter77 and karl_jones like this.
  40. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,324
    yep, Karl is doing an incredible job.
     
    Novack, karl_jones and codestage like this.
  41. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    Absolutely! Looking for more new miracles now! :rolleyes:
     
    karl_jones likes this.
  42. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    Ok the fix has now landed in all versions!

    2022.1.0a12
    2021.2.3f1
    2020.3.22f1
    2019.4.33f1

    :)
     
    codestage likes this.
  43. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    It seems current 2022.1 alpha doesn't have UNITY_2021_3_OR_NEWER conditional yet, hope it'll appear closer to the 2022.1 release, and it's pity we don't have *_OR_NEWER conditionals for every patch =D

    So far:
    Code (CSharp):
    1. [CustomPropertyDrawer(typeof(MyProperty), true)]
    2. public class MyPropertyDrawer
    3. {
    4.   // ...
    5.  
    6.   private void DrawHint(Rect rect, SerializedProperty property)
    7.   {
    8. #if UNITY_2021_3_OR_NEWER || UNITY_2022_1_OR_NEWER
    9.     var text = property.tooltip;
    10. #else
    11.     var text = property.GetTooltipWithReflection<MyProperty>();
    12. #endif
    13.     DrawHint(rect, text);
    14.   }
    15. }
    16.  
    17. internal static class SerializedPropertyExtensions
    18. {
    19. #if !(UNITY_2021_3_OR_NEWER || UNITY_2022_1_OR_NEWER)
    20.   public static string GetTooltipWithReflection<T>(this SerializedProperty property)
    21.   {
    22.     var field = typeof(T).GetField(property.name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
    23.     if (field == null)
    24.       return null;
    25.  
    26.     var attribute = field.GetCustomAttribute<TooltipAttribute>(true);
    27.     return attribute?.tooltip;
    28.   }
    29. #endif
    30. }
    31.  
    UNITY_2022_1_OR_NEWER can be removed when (if) 2022.1.0f1 will get UNITY_2021_3_OR_NEWER conditional.
     
  44. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,030
    Do you use asmdefs? You can specify defines based on patches using them.
     
    codestage likes this.
  45. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    Yes! But I don't see "Unity" Resource in the Version Defines packages list (only built-in modules are shown there) or you didn't mean Version Defines feature?..
     
  46. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    Ah, got it, that's because I'm on Unity 2020.2 where "Unity" Resource wasn't added yet but this is a min version I'm going to support with that feature where I'm using tooltips in Property Drawers :')
     
    karl_jones likes this.
  47. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,844
    Added expressions at 2020.3.0, and "Unity" appeared at 2020.2.0 as well but just didn't work which is not a problem for me as min version I'm interested in is 2020.3.22 where Unity resource works perfectly fine, yey, thanks @karl_jones (again)!

    upload_2021-11-11_14-40-40.png

    This allowed me to replace ugly
    UNITY_2021_3_OR_NEWER || UNITY_2022_1_OR_NEWER
    with single
    TOOLTIP_FIXED
    conditional ^^
     
    Last edited: Nov 11, 2021
    karl_jones likes this.