Get text from Input field in Unity3D with C#

前端 未结 3 1941
情书的邮戳
情书的邮戳 2020-12-10 02:16

I\'m trying to get a text inside an inputField in Unity3D with C#.

I\'ve placed an inputField in my editor, renamed and tagged in:

3条回答
  •  离开以前
    2020-12-10 02:46

    Attach below monobehaviour script to your InputField gameObject:

    public class test : MonoBehaviour {
        void Start ()
        {
            var input = gameObject.GetComponent();
            var se= new InputField.SubmitEvent();
            se.AddListener(SubmitName);
            input.onEndEdit = se;
    
            //or simply use the line below, 
            //input.onEndEdit.AddListener(SubmitName);  // This also works
        }
    
        private void SubmitName(string arg0)
        {
            Debug.Log(arg0);
        }
    }
    

    See also below animation:

    enter image description here

提交回复
热议问题