Search Unity

Detecting InputField onFocus

Discussion in 'UGUI & TextMesh Pro' started by tmendez, Mar 8, 2017.

  1. tmendez

    tmendez

    Joined:
    Oct 12, 2015
    Posts:
    39
    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!
     
  2. tmendez

    tmendez

    Joined:
    Oct 12, 2015
    Posts:
    39
    After some more tinkering I figured it out:

    Code (csharp):
    1.  
    2. public class ClearPlaceholderOnFocus : MonoBehaviour, ISelectHandler {
    3.     public Text placeholderText;
    4.  
    5.     public void OnSelect(BaseEventData data) {
    6.         placeholderText.text = "";
    7.     }
    8. }
    9.  
    Add this script to the InputField, and it will hide the placeholder when you click on it
     
    Last edited: Oct 29, 2020
  3. taxvi

    taxvi

    Joined:
    Feb 18, 2013
    Posts:
    30
    thanks for taking time to put this up here!
     
    tmendez likes this.
  4. himanshu817

    himanshu817

    Joined:
    Dec 24, 2018
    Posts:
    1
    thanks!
     
    tmendez likes this.
  5. Vampsz

    Vampsz

    Joined:
    Oct 18, 2018
    Posts:
    13
    thanks works like a charm
     
    tmendez likes this.
  6. habib32

    habib32

    Joined:
    Nov 5, 2018
    Posts:
    2
    Thanks brother, u saved my day
     
    tmendez likes this.
  7. edwon

    edwon

    Joined:
    Apr 24, 2011
    Posts:
    266
    you need these at the top of the script above

    using UnityEngine.EventSystems;
    using UnityEngine.UI;

    should save someone a few minutes of googling
     
    ryzeonline likes this.
  8. tucaz85710

    tucaz85710

    Joined:
    Jan 8, 2018
    Posts:
    14
    Thanks TMendez and edwon. Worked great and went from search to implement in less than 5 minutes.
     
    tmendez likes this.
  9. TOLGA-YAVUZ

    TOLGA-YAVUZ

    Joined:
    Feb 9, 2014
    Posts:
    4
    It is not working on first click, any idea how to fix that?

    using 2017.2.0f3 version.