Tanoda
InputFieldEnterSubmit.cs
Go to the documentation of this file.
1
3
5
7{
11 [RequireComponent(typeof(InputField))]
12 [AddComponentMenu("UI/Extensions/Input Field Submit")]
13 public class InputFieldEnterSubmit : MonoBehaviour
14 {
15 [System.Serializable]
16 public class EnterSubmitEvent : UnityEvent<string>
17 {
18
19 }
20
22 public bool defocusInput = true;
23 private InputField _input;
24
25 void Awake()
26 {
27 _input = GetComponent<InputField>();
28 _input.onEndEdit.AddListener(OnEndEdit);
29 }
30
31 public void OnEndEdit(string txt)
32 {
33 if (!Input.GetKeyDown(KeyCode.Return) && !Input.GetKeyDown(KeyCode.KeypadEnter))
34 return;
35 EnterSubmit.Invoke(txt);
36 if (defocusInput)
37 {
38 UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(null);
39 }
40 }
41 }
42}
Usage: Add this component to the input and add the function to execute to the EnterSubmit event of th...
Credit Erdener Gonenc - @PixelEnvision.