unity-game-enginetextmeshpro

TMP_InputField : in inspector can add method calls for things like onvaluechanged, but not for onsubmit?


https://docs.unity3d.com/Packages/com.unity.textmeshpro@1.3/api/TMPro.TMP_InputField.html#TMPro_TMP_InputField_onSubmit

here is the onsubmit method, however this is not exposed in the editor, only onvaluechanged, onselect, onendedit, ondeselect are exposed.

enter image description here

I would be ok to use onendedit, however I use this inputfield for a chatbox and if the user presses ESCAPE to abort his chat, currently onendedit will still trigger and the message gets sent. I want something that is only triggered when the user pressed ENTER, and I guess this would be covered by onsubmit however its not being exposed in the inspector for some reason.

Any advice appreciated. Thanks.


Solution

  • You will have to add this with code. Heres a one line way to make stuff happen when they submit.

        Start()
        {
            GetComponent<TMP_InputField>().onSubmit.AddListener((string input)=>{DoThing();});
        }
    

    Easier to understand version...

        public TMP_InputField MyInputfield
    
        Start()
        {
            MyInputfield.onSubmit.AddListener(DoStuffWhenSubmitted);
        }
        
        void DoStuffWhenSubmitted(string input)
        {
            // do stuff here
        }