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.
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.
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.
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);
}
}
}
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.