Use an action to notify my Manager that an InputField has been modified

Hello,

As explained in the title.

I want to put a script on my TMP input field so that when the text is changed (or maybe some other event as I don’t want the event to launch at each key stroke) an action is filled with some data (made a class for it) in order for a manager to act on that event.

How do I do that first script: listen to which component’s which event (onDataUpdated?) so that I can Action it myself with my own data (that I know how to do) so that my manager acts on it (I know that too).

Thanks in advance.

Here is an example script that I use to test input field events.

Create a scene that includes a TMP Input Field and TextMeshProUGUI text object. Add this script to the TextMeshProUGUI text object and then set the public reference to the input field.

3457335–274098–InputFieldEventCheck.cs (3.53 KB)

2 Likes

I’m not sure I get this, should look into it further.

Also, how to do this with a button onClick?

OK I’ve found the way for catching button clicks.

Here’s how I’ve done FYI:

using System;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Button))]
[RequireComponent(typeof(Image))]
public class ButtonOnClickHandler : MonoBehaviour
{
    public Action<OnButtonClickedEvent> OnButtonClicked;

    private Button button;

    private void Awake()
    {
        button = GetComponent<Button>();
    }

    private void OnEnable()
    {
        button.onClick.AddListener(delegate { OnClickHandle(gameObject); });
    }

    private void OnDisable()
    {
        button.onClick.RemoveAllListeners();
    }

    private void OnClickHandle(GameObject gameObject)
    {
        // Dispatch event
        if (OnButtonClicked != null)
        {
            OnButtonClicked(new OnButtonClickedEvent(gameObject.name));
        }
    }
}

I put this cscript on the buttons I want to “survey” the click of.

OK, I’ve had an epiphany.

What is the difference between the TMP_inputField component in the root gameobject and the Text Mesh Pro UGUI component in the placeholder and text children gameobject?

OK, I figured the thing out! Thanks for the help! I’ll stop spamming now, ahah.

When working with the Input Field, you only want to access the parent TMP_InputField component as the other components are there for structural reasons and to show text.

For instance, imagine you are using an input field to get a password from a user. The TMP_InputField.text would contain the password while the underlying TMP_Text.text would contain “******” which is what is displayed instead of showing the password.

Ooooh, ok, I see. But for my event forwarding, what is in that example script is enough?

And as a rule of thumb to get/set the text value I do it through TMP_InputField.text ?

The example script I provided covers the essential events from the input field.

Correct. You would set the text on the TMP_InputField itself and not on the underlying text components. The Placeholder text is an exception since it is only used to display “placeholder” text.