Detecting InputField onFocus

InputField seems to only have OnValueChanged and OnEndEdit as events. I want something like OnBeginEdit or OnFocus to trigger when a user clicks on the InputField in order to clear the Placeholder text. I can simply use a script’s Update method to constantly check for this, but I want it to be triggered rather than unnecessarily loop for a check.

Does anyone know how I would go about doing this? Thanks!

After some more tinkering I figured it out:

public class ClearPlaceholderOnFocus : MonoBehaviour, ISelectHandler {
    public Text placeholderText;

    public void OnSelect(BaseEventData data) {
        placeholderText.text = "";
    }
}

Add this script to the InputField, and it will hide the placeholder when you click on it

10 Likes

thanks for taking time to put this up here!

1 Like

thanks!

1 Like

thanks works like a charm

1 Like

Thanks brother, u saved my day

1 Like

you need these at the top of the script above

using UnityEngine.EventSystems;
using UnityEngine.UI;

should save someone a few minutes of googling

1 Like

Thanks TMendez and edwon. Worked great and went from search to implement in less than 5 minutes.

1 Like

It is not working on first click, any idea how to fix that?

using 2017.2.0f3 version.