Deleting the auto generated code for a given custom element

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?

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.

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:

   private List<string> m_Choices = new List<string>() {"one", "two", "three" };

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:

        public override void SetValueWithoutNotify(string newValue)
        {
            Debug.Log("setting value without notify");
            base.SetValueWithoutNotify(newValue);
            m_Index = getChoiceIndex(newValue);
            m_Selection.text = newValue;
        }

The thing is before I used index vs m_Index and that could be the reason for the bug:

        [UxmlAttribute]
        public int index
        {
            get
            {
                return m_Index;
            }
            set
            {
                Debug.Log($"setting index {value}");
                if (value != m_Index)
                {
                    m_Index = value;
                    if (m_Index >= 0 && m_Index < choices.Count)
                    {
                        Debug.Log("assign value");
                        this.value = getChoice(m_Index);
                    }
                    else
                    {
                        Debug.Log("assign value");
                        this.value = string.Empty;
                    }
                }
            }
        }

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.

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.

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.

The location of the file won’t make any difference. Are you able to provide some steps to reproduce the issue?

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.

1 Like

The issue was caused by assigning text to the display label:

        public override void SetValueWithoutNotify(string newValue)
        {
            Debug.Log("setting value without notify");
            base.SetValueWithoutNotify(newValue);
            m_Index = getChoiceIndex(newValue);
            m_Selection.text = newValue;
        }

It’s a bubble issue I assume? How do I prevent the input fied to ignore these changes?

    public partial class Selector : BaseField<string>
        private Label m_Selection;
        public Selector(string label) : base(label, null)
        {
            m_Input = this.Q<VisualElement>(null, "unity-base-field__input");
            m_Input.AddToClassList(inputUssClassName);

            m_Selection = new();
            m_Selection.AddToClassList(selectionUssClassName);
            m_Input.Add(m_Selection);

Fixed with this:

            m_Selection.RegisterCallback<ChangeEvent<string>>(labelChanged);

and:

        private void labelChanged(ChangeEvent<string> evt)
        {
            evt.StopPropagation();
            focusController.IgnoreEvent(evt);
        }

Label implements INotifyValueChanged<string> explicitly.
So you need to do ((INotifyValueChanged<string>) m_Selection).SetValueWithoutNotify(newValue);

1 Like

Ah thanks, I didn’t know about that cast.

1 Like