Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to get Button OnPointerClick to work with TMP_Inputfield

Discussion in 'UGUI & TextMesh Pro' started by blamejane, Dec 15, 2019.

  1. blamejane

    blamejane

    Joined:
    Jul 8, 2013
    Posts:
    233
    I can't figure out how to hookup my UI controls so that they work like iMessage app.

    I have an TMP_inputfield, which is set to multi line with newline. The inputfield has a child button for the "submit". The button game object has a script attached which implements IPointerClickHandler, which processes the users text.

    On iOS and in Editor, the OnClick handler is called when the child button is clicked, however Android only the OnEndEdit of the inputfield is called, which just dismisses the touch keyboard.

    Also, like iMessage, I have a button to attach a photo taken and another to attached pic from gallery. On iOS and in Editor these buttons receive events and work accordingly, but on Android the inputfield simply calls onEndEdit and nothing further.

    On Android user must type some text, then click the send/submit (child button), which dismisses the touch keyboard, then they click send/submit again to actual send. Same with Photo attachment, it's basically 2 clicks to get the needed behavior.

    I haven't found anyone with my problem, so I'm wondering if I'm missing something.

    Any ideas?
     
  2. blamejane

    blamejane

    Joined:
    Jul 8, 2013
    Posts:
    233
    I'm using Unity 2019.2.9f1
    TMP version 2.01

    Created a new unity project, target android
    Default scene with UI canvas with the following hierarchy:



    The TMP_InputField has the following script:

    Code (CSharp):
    1. public class MyTestInputField : MonoBehaviour
    2. {
    3.     private TMP_InputField _inputField;
    4.  
    5.     void Awake()
    6.     {
    7.         //_inputField = transform.GetComponent<InputField>();
    8.         _inputField = transform.GetComponent<TMP_InputField>();
    9.         if (_inputField == null)
    10.             throw new System.NullReferenceException("MyTestInputField : Awake() - failed to get InputField component");
    11.     }
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.  
    17.     }
    18.  
    19.     void OnEnable()
    20.     {
    21.         ValueChangeCheck();
    22.  
    23.         _inputField.onValueChanged.AddListener(delegate
    24.         {
    25.             ValueChangeCheck();
    26.         });
    27.         _inputField.onEndEdit.AddListener(delegate
    28.         {
    29.             Debug.Log("MyTestInputField : onEndEdit() - listener called");
    30.  
    31.             HandleDoneButtonClicked();
    32.  
    33.         });
    34.  
    35.         _inputField.onSubmit.AddListener(delegate
    36.         {
    37.             Debug.Log("MyTestInputField : onSubmit() - listener called");
    38.         });
    39.  
    40.     }
    41.  
    42.     void OnDisable()
    43.     {
    44.         Debug.Log("MyTestInputField : OnDisable() - remove all listeners");
    45.         _inputField.onEndEdit.RemoveAllListeners();
    46.         _inputField.onValueChanged.RemoveAllListeners();
    47.         _inputField.onSubmit.RemoveAllListeners();
    48.     }
    49.  
    50.  
    51.     public void ValueChangeCheck()
    52.     {
    53.         ////        // Debug.Log("PipInputFieldModelView : ValueChangeCheck()");
    54.         //if (_inputField.text != null && _inputField.text != "")
    55.         //    Controller.HandleInputFieldValueChanged(true);
    56.         //else
    57.         //Controller.HandleInputFieldValueChanged(false);
    58.     }
    59.  
    60.     public void HandleDoneButtonClicked()
    61.     {
    62.         Debug.Log("MyTestInputField : HandleDoneButtonClicked()");
    63.  
    64.         //_inputField.DeactivateInputField();
    65.     }
    66. }
    67.  
    The save button has the following script:

    Code (CSharp):
    1. public class MyTestSaveButton : MonoBehaviour, IPointerClickHandler, ISubmitHandler
    2. {
    3.     private Button _button;
    4.     private string _message;
    5.  
    6.     void Awake()
    7.     {
    8.         _button = transform.GetComponent<Button>();
    9.         if (_button == null)
    10.             throw new System.NullReferenceException("MyTestSaveButton : Awake() - failed to get Button component");
    11.     }
    12.  
    13.     #region IPointerClickHandler implementation
    14.  
    15.     void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
    16.     {
    17.         Debug.Log("MyTestSaveButton : OnPointerClick()");
    18.  
    19.         StartCoroutine(DisplayMessage("SAVE POINTER CLICKED 111"));
    20.  
    21.     }
    22.  
    23.     public void OnSubmit(BaseEventData eventData)
    24.     {
    25.         Debug.Log("MyTestSaveButton : OnSubmit()");
    26.  
    27.         StartCoroutine(DisplayMessage("SAVE SUBMITTED 222"));
    28.  
    29.     }
    30.  
    31.     public void Display(string msg)
    32.     {
    33.         StartCoroutine(DisplayMessage(msg));
    34.  
    35.     }
    36.  
    37.     #endregion
    38.  
    39.     private IEnumerator DisplayMessage(string msg)
    40.     {
    41.         _message = msg;
    42.         yield return new WaitForSeconds(1.5f);
    43.         _message = "";
    44.     }
    45.  
    46.     void OnGUI()
    47.     {
    48.  
    49.         GUIStyle style = new GUIStyle();
    50.         style.richText = true;
    51.         GUILayout.Space(500f);
    52.         GUILayout.Label("<size=50><color=gray>" + _message + "</color></size>", style);
    53.     }
    54. }
    55.  
    Running the app on Android Device Samsung PH1.

    Still don't understand why the submit/save button fails to display OnGui Message when clicked. To see message you have to type text in input field, click save, click save again.

    Anyone have any ideas how I can get the button to respond on first click?

    I'm assuming I'll have abandon TMP, which I am using for emoji support, and use plain old Unity Inputfield.

    Love to hear some thoughts on this.

    Thanks everyone.

    EDIT: One thing I should mention again is I have a few buttons (like attach a photo, and submit) all need to work with 1 click, not 2.
     
  3. blamejane

    blamejane

    Joined:
    Jul 8, 2013
    Posts:
    233
    Could someone please try a regular old unity inputfield on android, with a button next to it or above, or whatever. Tell me what happens when you enter some text and click the button. Does the button respond as soon as you click?

    c'mon people, can't one person help me? I swear I see a million threads where people get a response, and for whatever reason, nobody ever bothers on my problems. If my saying this offends, then fine. I don't imagine I'll get any help what-so-ever on this problem anyway. Sorry to vent, but I've spent a freakin week on this stupid TMP/ inputfileld button crap and I can't seem to get anything to work. I'm seriously stupid I guess.