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

Resolved Deleting the auto generated code for a given custom element

Discussion in 'UI Toolkit' started by dlorre, May 11, 2023.

  1. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I am making a custom element and noticed that there was some kind of auto generated code that seems to give it some default values. I'm not sure what it does exactly but I've had some initialization issues with it.

    I got it to refresh by deleting the Library folder but is there a faster/simpler way?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    Can you explain more what the issues are? This is a new feature so its possible its a bug. The default values come from the custom elements fields.
     
  3. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Well, I have made a kind of dropdown field adapted for gamepad, so just a label with 2 buttons left and right. I've had issues with the choices field because when I started I hardcoded them in the custom element, something like:

    Code (csharp):
    1.  
    2.    private List<string> m_Choices = new List<string>() {"one", "two", "three" };
    3.  
    Then even after I deleted this initialization it kept using it, and the Debug Console message was mentioning a Generator file which I didn't find anywhere. Unfortunately I can't remember the exact details.

    Now I have an issue with SetValueWithoutNotify() triggering a ChangeEvent:

    Code (csharp):
    1.  
    2.         public override void SetValueWithoutNotify(string newValue)
    3.         {
    4.             Debug.Log("setting value without notify");
    5.             base.SetValueWithoutNotify(newValue);
    6.             m_Index = getChoiceIndex(newValue);
    7.             m_Selection.text = newValue;
    8.         }
    9.  
    The thing is before I used index vs m_Index and that could be the reason for the bug:

    Code (csharp):
    1.  
    2.  
    3.         [UxmlAttribute]
    4.         public int index
    5.         {
    6.             get
    7.             {
    8.                 return m_Index;
    9.             }
    10.             set
    11.             {
    12.                 Debug.Log($"setting index {value}");
    13.                 if (value != m_Index)
    14.                 {
    15.                     m_Index = value;
    16.                     if (m_Index >= 0 && m_Index < choices.Count)
    17.                     {
    18.                         Debug.Log("assign value");
    19.                         this.value = getChoice(m_Index);
    20.                     }
    21.                     else
    22.                     {
    23.                         Debug.Log("assign value");
    24.                         this.value = string.Empty;
    25.                     }
    26.                 }
    27.             }
    28.         }
    29.  
    But now I have fixed that and the error persists so I wanted to know how to delete the generator file. It seems that it was not in Library I can't remember how I fixed it the first time.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    You should not need to delete the generator file, it gets updated when your code changes.

    The new system will take this as the default value
    private List<string> m_Choices = List<string>() {"one", "two", "three" };

    If it's still using it after you deleted it then that's a bug, so please file a bug report so we can get it fixed.
     
  5. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Thanks, but I have fixed that one, the actual issue is with SetValueWithoutNotify, and since I'm in the dark concerning this generator file I can't make a side project to send as bug report.

    My file is not in Assets/Scripts but in Assets/PackageName/Scripts if that may help debugging on your side.
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    The location of the file won't make any difference. Are you able to provide some steps to reproduce the issue?
     
  7. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I'm not sure that my actual issue is related to the generator, this is what I wanted to check. Anyway I have deleted Library, obj and the cache folders in AppData and the issue persists so it's probably unrelated even though it makes no sense that the ChangeEvent is triggered like that.

    For the previous issue that is fixed, it was with a previous version of Unity I'm not even sure that it is still an issue and I can't remember the exact steps on how to reproduce it. All I remember is that I had an error at the initialization and it was mentioning a generator file that I could not find anywhere. I probably should have sent a bug report at that time though.
     
    karl_jones likes this.
  8. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    The issue was caused by assigning text to the display label:

    Code (csharp):
    1.  
    2.         public override void SetValueWithoutNotify(string newValue)
    3.         {
    4.             Debug.Log("setting value without notify");
    5.             base.SetValueWithoutNotify(newValue);
    6.             m_Index = getChoiceIndex(newValue);
    7.             m_Selection.text = newValue;
    8.         }
    9.  
    It's a bubble issue I assume? How do I prevent the input fied to ignore these changes?

    Code (csharp):
    1.  
    2.     public partial class Selector : BaseField<string>
    3.  
    Code (csharp):
    1.  
    2.         private Label m_Selection;
    3.  

    Code (csharp):
    1.  
    2.         public Selector(string label) : base(label, null)
    3.         {
    4.             m_Input = this.Q<VisualElement>(null, "unity-base-field__input");
    5.             m_Input.AddToClassList(inputUssClassName);
    6.  
    7.             m_Selection = new();
    8.             m_Selection.AddToClassList(selectionUssClassName);
    9.             m_Input.Add(m_Selection);
    10.  
     
  9. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Fixed with this:

    Code (csharp):
    1.  
    2.             m_Selection.RegisterCallback<ChangeEvent<string>>(labelChanged);
    3.  
    and:
    Code (csharp):
    1.  
    2.         private void labelChanged(ChangeEvent<string> evt)
    3.         {
    4.             evt.StopPropagation();
    5.             focusController.IgnoreEvent(evt);
    6.         }
    7.  
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,820
    Label implements
    INotifyValueChanged<string>
    explicitly.
    So you need to do
     ((INotifyValueChanged<string>) m_Selection).SetValueWithoutNotify(newValue);
     
    dlorre likes this.
  11. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Ah thanks, I didn't know about that cast.
     
    karl_jones likes this.