UIBuilder: Alternate shorter AddTask?

I had written this and it seems to work

void AddTask(KeyDownEvent e)
{
    if (e.keyCode == KeyCode.Return)  AddTask();
 }
2 Likes

Yep, that works. It’s nice and simple too! :slight_smile:
You could also try modifying things to work with the new input system if you like. That can take a little bit of effort but isn’t super difficult. In general, I like to try and use the new input system whenever I can these days, but throwing that into the course was a little off topic.
Like everything in Unity, there’s always more than one way to do pretty much anything! :joy:

Even quicker…

  • Check the “Is Delayed” button on the text field. This will make RegisterValueChangedCallback only fire when you leave the field or hit enter.
  • Instead of registering a KeyDownEvent (which requires a check every keystroke): write this
        taskText.RegisterValueChangedCallback((evt) =>
        {
            AddTask();
        })

Unfortunately, while that trick works flawlessly for TextField, IntegerField and FloatField, it does not work at all on Slider or SliderInt as neither one exposes IsDelayed.

I found that using RegisterValueChangeCallback will also add the task when you click away from the text field, which isn’t always what you want. Explicitly listening for the return key gives you some added flexibility and means you can be a bit more explicit about when you submit.

However, RegisterValueChangeCallback will be the perfect option for when we come to searching for tasks in our list, so we will see it in action later in the course :slight_smile:

2 Likes

I hadn’t thought of that last part. In the InventoryItemEditor, I make extensive use of the RegisterValueChangeCallback because generally in a property, you do want to commit the field once the control leaves focus.

2 Likes

Privacy & Terms