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

Bug Setting label of PropertyField is ignored

Discussion in 'UI Toolkit' started by Tom-Atom, Jan 2, 2022.

  1. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    Hi, I found I can't change label of PropertyFiled with PropertyFiled.label = "whatever" in PropertyDrawer. These calls are ignored and label is not changed (except for initial setup in CreatePropertyGUI()). I am on 2021.2.7f1.

    I have simple class, that holds single float value and enum saying what this value does - like this:

    Code (CSharp):
    1. public enum DistOrTimeType { Distance, Time }
    2.  
    3. [System.Serializable]
    4. public class DistOrTime {
    5.  
    6.     public DistOrTimeType type;
    7.     public float value = 0;
    8. }
    9.  
    When this calss is used as property of MonoBehaviour with PropertyDrawer code bellow, it looks like this:

    PropertyDrawer.png
    What I want is to 1. select type of value in Type field and based on type change the label of second field.
    In code bellow I set label for value when creating PropertyField and it works. But then, when Type is changed, setting PropertyField.label (inside local function Repaint) is ignored. Expected result is that label is changed.

    I found workaround, how to do what I need. In bellow code it is also in Repaint() local function and it is commented out now. I can retreive Label element through query and then change its label text. But I believe, that PropertyFiled.label should work instead of this ugly way.

    This is code for PropertyDrawer:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.UIElements;
    3. using UnityEngine.UIElements;
    4.  
    5. [CustomPropertyDrawer(typeof(DistOrTime))]
    6. public class DistOrTimeDrawer : PropertyDrawer {
    7.  
    8.     public override VisualElement CreatePropertyGUI(SerializedProperty property) {
    9.  
    10.         var container = new VisualElement();
    11.  
    12.         // properties
    13.         SerializedProperty typeProp = property.FindPropertyRelative("type");
    14.         SerializedProperty valueProp = property.FindPropertyRelative("value");
    15.  
    16.         // fields
    17.         var typeField = new PropertyField(typeProp);
    18.         var valueField = new PropertyField(valueProp) { label = GetLabel() };
    19.  
    20.         // compose container
    21.         container.Add(typeField);
    22.         container.Add(valueField);
    23.  
    24.         // callbacks
    25.         typeField.RegisterCallback<ChangeEvent<string>>(evt => Repaint());
    26.  
    27.         return container;
    28.  
    29.  
    30.         string GetLabel() {
    31.             var type = (DistOrTimeType)typeProp.intValue;
    32.             return type == DistOrTimeType.Distance ? "Distance in units" : "Time in seconds";
    33.         }
    34.  
    35.         void Repaint() {
    36.  
    37.             // Not working
    38.             valueField.label = GetLabel();
    39.  
    40.             // Workaround - working
    41.             //if (valueField.Q(className: "unity-base-field__label") is Label pLabel) {
    42.             //    pLabel.text = GetLabel();
    43.             //}
    44.         }
    45.     }
    46. }
    47.  
     
    a436t4ataf likes this.
  2. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,873
    I'm seeing same bug n 2019 with the constructor for PropertyField. But it doesn't matter when I set the value of label, it is always ignored by PropertyField.

    It looks like someone at Unity fixed 1/4 of the bug - label being ignored entirely - but not the whole bug. After creating a PropertyField and adding it, if I query it for a Label I get null:
    Q<Label>() // always null
     
  3. fipsy

    fipsy

    Joined:
    Sep 3, 2016
    Posts:
    13
    I'm having the same problem on 2021.2.14f1

    @Tom-Atom do you mind creating a bug report? It's usually faster than waiting in the forums
     
  4. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,873
    Create it yourself?

    I'm currently porting to 2020 LTS and PropertyField is *less* buggy here but still has some bugs. I haven't got as far as testing on latest LTS (I have to support the oldest versions possible, so I'm updating my workarounds for this bug). If it's still broken on 2021 it would be a huge help if you logged a bug directly in the 2021 version to get it looked at ASAP.