InputField: onValueChanged missing?

Hmm, am I missing something or is there no onTextChanged or onValueChanged event for input fields (like onValueChanged for sliders)?

I’m kind of awkwardly staring at my screen not knowing how to activate the login button once the inputfield text is valid (without constantly polling the text value). I checked the documentation for InputField but onValidateInput and onSubmit aren’t helpful.

Only took a quick look and maybe it will help until someone can give you a proper way.

I put a simple script on the text object of the InputField.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class InputTest : MonoBehaviour
{
    public Text text;

    public void ValidateText()
    {
        //If valid enable login button...
        Debug.Log (text.text);
    }
}

Add an “Event Trigger” to the InputField
Add New, UpdateSelected
setup object and function

So this will call that function I think based on “Input Actions Per Second” constantly while the input field is selected.

hacky, so maybe someone knows the correct way.

2 Likes
public class InputTest : MonoBehaviour
{
    UnityEngine.UI.InputField field;
    void Start ()
    {
        field.onSubmit.AddListener(new UnityEngine.Events.UnityAction<string>(OnSubmitField));
        //Or
        field.onSubmit.AddListener(new UnityEngine.Events.UnityAction<string>((string value) => { Debug.Log(value); }));
    }


    void OnSubmitField(string value)
    {
        Debug.Log(value);
    }
}
3 Likes

Yes this is a bug that was fixed after b17 was already in the build process. It should be fixed for the next beta :slight_smile:

There is an onSubmit that exists on the InputField… but we forgot to include it in the inspector. We have added it now.

@_Adriano
@Tim-C

Like I said in my initial post, onSubmit doesn’t do the job. It is only triggered when pressing Return or the key set up in the Event System. I am looking for a way to be notified of a text value change instantly without polling, e.g. to activate / deactivate certain buttons.

The most intuitive way would be by using the same system the slider component already uses in the inspector: onValueChanged.

The class of UGUI can be inherited?

using UnityEngine;
using UGui = UnityEngine.UI;
public class MyCustomInput : UGui.InputField
{
    protected override void Awake()
    {
        Debug.Log("<color=red> Custom </color>");
        base.Awake();
    }
}

I agree that there should be an “OnValueChanged” right out of the box that we can register for in the editor.

There are KeyUp, KeyDown, Changed etc. events defined for Event. It would be nice if InputFields emitted them. It would also be nice if you could get an Event object sent to your event handlers in a simple and obvious way.

And Carthage should be burned.

Well, here’s a little hack…
EDIT: changed to use UnityAction<> instead.

using UnityEngine.EventSystems;
using UnityEngine.UI;

public class CustomInputField : InputField
{
    public UnityAction<string> OnValueChanged;

    public override void OnUpdateSelected(BaseEventData eventData)
    {
        var old = this.value;
        base.OnUpdateSelected(eventData);
        if (this.value != old && OnValueChanged != null) {
            OnValueChanged(this.value);
        }
    }
}

@Greg-SS

Use unityaction <> instead of custom delegate. Thanks for idea though

Good news from b21:

I still have a problem though. The event is not called when deleting a character.
Is this the desired behaviour? Technically, when deleting a character, the value changed.

For example, I’m using this in a search bar updating the results as the string is written. When deleting the last inserted character, it should call the OnValueChange callback so that I can update the results filtered by the remaining text but it’s not being called.
I’ve been using the OnUpdateSelected to work around this.

Added it to deleting chars as well. Its was a oversight by me :frowning:

No problem phil. Thanks for the fix. :slight_smile: