From cdde18f8d08c33dcd4f34525a6a858249e00f8d4 Mon Sep 17 00:00:00 2001 From: Vicente Russo Date: Sat, 19 Sep 2015 02:43:45 -0300 Subject: [PATCH] A minimalist working alternative for the ReturnKeyTriggersButton component --- Scripts/InputFieldEnterSubmit.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Scripts/InputFieldEnterSubmit.cs diff --git a/Scripts/InputFieldEnterSubmit.cs b/Scripts/InputFieldEnterSubmit.cs new file mode 100644 index 0000000..a1d6873 --- /dev/null +++ b/Scripts/InputFieldEnterSubmit.cs @@ -0,0 +1,30 @@ +/// Usage: Add this component to the input and add the +/// function to execute to the EnterSubmit event of this script + +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.Events; + +[RequireComponent(typeof(InputField))] +public class InputFieldEnterSubmit : MonoBehaviour +{ + + [System.Serializable] + public class EnterSubmitEvent : UnityEvent { } + + public EnterSubmitEvent EnterSubmit; + private InputField _input; + + void Awake() + { + _input = GetComponent(); + _input.onEndEdit.AddListener(OnEndEdit); + } + + public void OnEndEdit(string txt) + { + if (!Input.GetKeyDown(KeyCode.Return) && !Input.GetKeyDown(KeyCode.KeypadEnter)) return; + EnterSubmit.Invoke(txt); + } + +}