Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

InputField OnSubmit

Discussion in 'UGUI & TextMesh Pro' started by MuNgLo, Aug 21, 2014.

  1. MuNgLo

    MuNgLo

    Joined:
    Jan 27, 2014
    Posts:
    31
    I am looking for an example of how to grab the OnSubmit (and the others) events from InputField. Can't find any and I don't seem to be able to wrap my head around it without an example to read.
    There is no example of it in the demoproject and I would suggest adding one.


    ---EDIT---
    So I found the solution. Leaving a simple example here so it can help someone else.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class GConsoleuGUIInput : MonoBehaviour {
    5.  
    6.     void Start() {
    7.         GetComponent<InputField> ().onSubmit.AddListener (OnSubmit);
    8.         }
    9.     void OnSubmit(string line) {
    10.         Debug.Log ("OnSubmit("+line+")");
    11.     }
    12. }
     
    Last edited: Aug 21, 2014
    Ali-Nagori likes this.
  2. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    294
    I take it you have the same problem as I have, which is InputField being defocused when trying to add new line with Enter/Return? Logically pressing Enter while typing text into a multiline InputField should make a newline (Shift-Enter does that, but how many users know that?). I'm aware that EventSystem has Submit-button mapped to Enter/Return, but even when setting that to something else, the problem persists. I have filed a bug report, let's see how it goes.

    Sorry for not helping on your actual problem, I've yet to look into that myself.. :)
     
  3. MuNgLo

    MuNgLo

    Joined:
    Jan 27, 2014
    Posts:
    31
    Ctrl-enter is something that works in steamchat so it isn't that far of from what users might find by experimenting.

    But no. I am trying to trigger some code when you press enter to submit the thing you wrote. For console input to be precise.
    So for me to be able to do what I need I need to be able to track any changes in inputvalue(each keypress) and submit. Oh and if the TextField have focus to actually. Will need that to make arrows up/down work I guess.

    Problem is I haven't done anything yet with such an eventsystem so I need to read some example code. I bet it isn't hard once you know how to do it but I can't find the info.

    ---EDIT---
    Found the solution. See OP for edit
     
    Last edited: Aug 21, 2014
  4. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    Why they don't expose this on the InputField inspector? Also, how to do this implementing the interface ISubmitHandler? I can't get it to work...
     
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    It's on their TODO list. I think they said that you will be able to drag'n'drop the InputField event just like Buttons and the rest.
     
    Last edited: Sep 22, 2014
  6. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,196
    They just did it! It's on the b19 release notes! Thanks a lot UT!
     
  7. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Good, glad to hear that.