Search Unity

"OnSubmit" Doesn't fire on InputField

Discussion in 'UGUI & TextMesh Pro' started by BonyYousuf, Oct 13, 2014.

  1. BonyYousuf

    BonyYousuf

    Joined:
    Aug 22, 2013
    Posts:
    110
    Hi,
    I am using b20 beta on a mac. I used a InputField but it is not firing the "OnSubmit" field. I even added the "EventTrigger" field at some point and managed to work with "PointerClick". I am saying this to show that I do know how the new system works. But I could not make the "OnSubmit" work. I pressed Enter, ctrl+enter, shift+enter. Nothing works.

    Please help.
    Thanks
     
  2. crag

    crag

    Joined:
    Dec 13, 2013
    Posts:
    145
  3. poolts

    poolts

    Joined:
    Aug 9, 2012
    Posts:
    113
    I'm on Mac beta 20 and the callback is as follows:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         m_inputText.onSubmit.AddListener(ProcessSubmit);
    4.     }
    5.  
    6.     public void ProcessSubmit(string input)
    7.     {
    8.         print(input)
    9.     }
     
  4. BonyYousuf

    BonyYousuf

    Joined:
    Aug 22, 2013
    Posts:
    110
    Are you sure this is working for you on mac? Cause I just tried it with no luck.

    Code (CSharp):
    1. void Start () {
    2.         GetComponent<InputField>().onSubmit.AddListener(ProcessSubmit);
    3.     }
    4.    
    5.     public void ProcessSubmit(string input)
    6.     {
    7.         Debug.Log(input);
    8.     }
     
  5. BonyYousuf

    BonyYousuf

    Joined:
    Aug 22, 2013
    Posts:
    110
    @crag bro you are giving me legacy UI code. I want to use the UI that came with 4.6
     
  6. crag

    crag

    Joined:
    Dec 13, 2013
    Posts:
    145
    sorry brochacho
     
  7. BonyYousuf

    BonyYousuf

    Joined:
    Aug 22, 2013
    Posts:
    110
    Found the issue. I had multiline turned on, which prevents the OnSubmit on pressing Enter.
     
    JaggerMcClaw and poolts like this.
  8. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    This doesn't work anymore with RC2. onSubmit has been taken out of InputField.. I need to be able to add a listener to it, but for the life of me cannot figure out how.
     
  9. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    its now call onEndEdit. as stated in the release notes.
     
  10. -chris

    -chris

    Joined:
    Mar 1, 2012
    Posts:
    99
    No OnSubmit()? Is that why this code compiles fine but seemingly does nothing? Or am I missing something?

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using System.Collections;
    4.  
    5. public class TextHelper : MonoBehaviour, ISubmitHandler {
    6.  
    7.     public void OnSubmit(BaseEventData data) {
    8.         Debug.Log("Submitted!");
    9.     }
    10.  
    11. }
    I've tried attaching this to an InputField component and a Text component but it doesn't fire.
     
  11. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    If you made your TextHelper class inherit from InputField, it should work I believe... Your OnSubmit function may need to be an override void as well.
     
  12. -chris

    -chris

    Joined:
    Mar 1, 2012
    Posts:
    99
    Thanks for your reply! Replaced the InputField component on the game object with this and linked up the Text component but no luck :(

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.EventSystems;
    4. using System.Collections;
    5.  
    6. public class TextHelper : InputField, ISubmitHandler {
    7.  
    8.     public override void OnSubmit(BaseEventData data) {
    9.         Debug.Log("Submitted!");
    10.     }
    11.  
    12. }
     
  13. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Here's a good read for you:

    InputField: onValueChanged missing?

    also, I don't know if you have to pull from their repository to do this, or if you're able to do it out-of-the-box. I don't yet know how people are overriding OnSubmit, or if there is anything else required before you can override it, but it looks like your source should be working.
     
  14. forcepusher

    forcepusher

    Joined:
    Jun 25, 2012
    Posts:
    227
    Right now I'm on 4.6.2 and it's calling the event only when I hit enter 2 times. Any ideas how to fix that without polling for "Enter" button press ?
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class Test : MonoBehaviour, ISubmitHandler
    5. {
    6.     public void OnSubmit(BaseEventData eventData)
    7.     {
    8.         Debug.Log("ONSUBMIT");
    9.     }
    10. }
    BTW it doesn't call that submit event on android at all.
    Using onEndEdit ain't solution, because it calls that event even if you click away or hit escape.
     
    Last edited: Feb 11, 2015
    Glader and mr-sambarlow like this.
  15. XRA

    XRA

    Joined:
    Aug 26, 2010
    Posts:
    265
    same issue... only calls the event after hitting enter multiple times, pretty frustrating
     
  16. Manny3

    Manny3

    Joined:
    May 20, 2016
    Posts:
    14
    Bump..

    Didn't anyone ever find a good response to this?

    As mentioned OnEndEdit also activates when you click away from screen which is not the desired result.

    OnSubmit seems like the obvious solutions but it just doesn't seem to do anything.
     
  17. Brogan89

    Brogan89

    Joined:
    Jul 10, 2014
    Posts:
    244
    Hey I've ran into this problem too. I think I've found a simple fix. No wizardry involved.

    It will confirm when you press Enter but wont if you click outside the InputField bounds. Haven't tested on mobile but might help a few people out.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. public class InputFieldEx : InputField
    7. {
    8.     [System.Serializable]
    9.     public class OnConfirm : UnityEvent<string> { }
    10.  
    11.     public OnConfirm onConfirm;
    12.  
    13.     protected override void Awake()
    14.     {
    15.         base.Awake();      
    16.         onEndEdit.AddListener(OnEndEdit);
    17.     }
    18.  
    19.     private void OnEndEdit(string value)
    20.     {
    21.         if (EventSystem.current.currentSelectedGameObject == gameObject &&
    22.             EventSystem.current.alreadySelecting == gameObject)
    23.         {
    24.             Debug.Log($"'{name}' Wasn't Confirmed");
    25.             return;
    26.         }
    27.  
    28.         Debug.Log($"Confirm: {value}");
    29.  
    30.         if (onConfirm != null)
    31.             onConfirm.Invoke(value);
    32.     }
    33.  
    34.     public void BringToFocus()
    35.     {
    36.         EventSystem.current.SetSelectedGameObject(gameObject, null);
    37.         OnPointerClick(new PointerEventData(EventSystem.current));
    38.     }
    39. }
    The Editor Sript
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.UI;
    3.  
    4. [CustomEditor(typeof(InputFieldEx))]
    5. public class E_InputFieldEx : InputFieldEditor
    6. {
    7.     SerializedProperty _onConfirm;
    8.  
    9.     protected override void OnEnable()
    10.     {
    11.         base.OnEnable();
    12.         _onConfirm = serializedObject.FindProperty("onConfirm");
    13.     }
    14.  
    15.     public override void OnInspectorGUI()
    16.     {
    17.         base.OnInspectorGUI();
    18.         serializedObject.Update();
    19.         EditorGUILayout.PropertyField(_onConfirm);
    20.         serializedObject.ApplyModifiedProperties();
    21.     }
    22. }
     
    Last edited: Aug 15, 2017