2015-09-20 07:24:17 +08:00
|
|
|
|
/// Credit Vicente Russo
|
2020-07-09 03:38:28 +08:00
|
|
|
|
/// Sourced from - https://bitbucket.org/SimonDarksideJ/unity-ui-extensions/issues/23/returnkeytriggersbutton
|
2015-09-19 13:43:45 +08:00
|
|
|
|
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
2015-09-20 07:24:17 +08:00
|
|
|
|
namespace UnityEngine.UI.Extensions
|
|
|
|
|
{
|
2015-09-20 08:21:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Usage: Add this component to the input and add the function to execute to the EnterSubmit event of this script
|
|
|
|
|
/// </summary>
|
|
|
|
|
[RequireComponent(typeof(InputField))]
|
|
|
|
|
[AddComponentMenu("UI/Extensions/Input Field Submit")]
|
|
|
|
|
public class InputFieldEnterSubmit : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[System.Serializable]
|
2019-02-22 23:03:59 +08:00
|
|
|
|
public class EnterSubmitEvent : UnityEvent<string>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2015-09-19 13:43:45 +08:00
|
|
|
|
|
2015-09-20 08:21:29 +08:00
|
|
|
|
public EnterSubmitEvent EnterSubmit;
|
2019-02-22 23:03:59 +08:00
|
|
|
|
public bool defocusInput = true;
|
2015-09-20 08:21:29 +08:00
|
|
|
|
private InputField _input;
|
2015-09-19 13:43:45 +08:00
|
|
|
|
|
2015-09-20 08:21:29 +08:00
|
|
|
|
void Awake()
|
|
|
|
|
{
|
|
|
|
|
_input = GetComponent<InputField>();
|
|
|
|
|
_input.onEndEdit.AddListener(OnEndEdit);
|
|
|
|
|
}
|
2015-09-19 13:43:45 +08:00
|
|
|
|
|
2015-09-20 08:21:29 +08:00
|
|
|
|
public void OnEndEdit(string txt)
|
|
|
|
|
{
|
2019-02-22 23:03:59 +08:00
|
|
|
|
if (!Input.GetKeyDown(KeyCode.Return) && !Input.GetKeyDown(KeyCode.KeypadEnter))
|
|
|
|
|
return;
|
2015-09-20 08:21:29 +08:00
|
|
|
|
EnterSubmit.Invoke(txt);
|
2019-02-22 23:03:59 +08:00
|
|
|
|
if (defocusInput)
|
|
|
|
|
{
|
|
|
|
|
UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(null);
|
|
|
|
|
}
|
2015-09-20 08:21:29 +08:00
|
|
|
|
}
|
2015-09-19 13:43:45 +08:00
|
|
|
|
}
|
2015-09-20 07:24:17 +08:00
|
|
|
|
}
|