如何在软键盘Unity3d中检测"done"按钮
How to detect "done" Button in softkeyboard Unity3d
我在我的 android 应用程序中使用 InputField 来获取一个字符串,当我输入一个字符串时会弹出一个软键盘,但现在我想在用户按下 "done" 按钮时调用一个函数在软键盘中我该怎么做?
Unity3d 4.6.2f1
你应该可以通过使用这个来达到同样的效果:
function Update () {
Event e = Event.currrent;
if (e.type == EventType.keyDown && e.keyCode == KeyCode.Return)
//Put in what you want here
}
我发现的最佳方法是将 InputField 子类化。您可以在 bitbucket 上查看 UnityUI 的源代码。在该子类中,您可以访问受保护的 m_keyboard 字段并检查完成是否已按下且未取消,这将为您提供所需的结果。使用 EventSystem 的 "submit" 无法正常工作。
将它集成到 Unity EventSystem 中时会更好。
像这样:
SubmitInputField.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine.Events;
using System;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
public class SubmitInputField : InputField
{
[Serializable]
public class KeyboardDoneEvent : UnityEvent{}
[SerializeField]
private KeyboardDoneEvent m_keyboardDone = new KeyboardDoneEvent ();
public KeyboardDoneEvent onKeyboardDone {
get { return m_keyboardDone; }
set { m_keyboardDone = value; }
}
void Update ()
{
if (m_Keyboard != null && m_Keyboard.done && !m_Keyboard.wasCanceled) {
m_keyboardDone.Invoke ();
}
}
}
Editor/SubmitInputFieldEditor.cs
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;
using UnityEditor.UI;
[CustomEditor (typeof(SubmitInputField), true)]
[CanEditMultipleObjects]
public class SubmitInputFieldEditor : InputFieldEditor
{
SerializedProperty m_KeyboardDoneProperty;
SerializedProperty m_TextComponent;
protected override void OnEnable ()
{
base.OnEnable ();
m_KeyboardDoneProperty = serializedObject.FindProperty ("m_keyboardDone");
m_TextComponent = serializedObject.FindProperty ("m_TextComponent");
}
public override void OnInspectorGUI ()
{
base.OnInspectorGUI ();
EditorGUI.BeginDisabledGroup (m_TextComponent == null || m_TextComponent.objectReferenceValue == null);
EditorGUILayout.Space ();
serializedObject.Update ();
EditorGUILayout.PropertyField (m_KeyboardDoneProperty);
serializedObject.ApplyModifiedProperties ();
EditorGUI.EndDisabledGroup ();
serializedObject.ApplyModifiedProperties ();
}
}
仍然适用于 2019.3!我只需要更改一些内容即可使用 TMP_InputField。
我在我的 android 应用程序中使用 InputField 来获取一个字符串,当我输入一个字符串时会弹出一个软键盘,但现在我想在用户按下 "done" 按钮时调用一个函数在软键盘中我该怎么做?
Unity3d 4.6.2f1
你应该可以通过使用这个来达到同样的效果:
function Update () {
Event e = Event.currrent;
if (e.type == EventType.keyDown && e.keyCode == KeyCode.Return)
//Put in what you want here
}
我发现的最佳方法是将 InputField 子类化。您可以在 bitbucket 上查看 UnityUI 的源代码。在该子类中,您可以访问受保护的 m_keyboard 字段并检查完成是否已按下且未取消,这将为您提供所需的结果。使用 EventSystem 的 "submit" 无法正常工作。 将它集成到 Unity EventSystem 中时会更好。
像这样: SubmitInputField.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine.Events;
using System;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
public class SubmitInputField : InputField
{
[Serializable]
public class KeyboardDoneEvent : UnityEvent{}
[SerializeField]
private KeyboardDoneEvent m_keyboardDone = new KeyboardDoneEvent ();
public KeyboardDoneEvent onKeyboardDone {
get { return m_keyboardDone; }
set { m_keyboardDone = value; }
}
void Update ()
{
if (m_Keyboard != null && m_Keyboard.done && !m_Keyboard.wasCanceled) {
m_keyboardDone.Invoke ();
}
}
}
Editor/SubmitInputFieldEditor.cs
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;
using UnityEditor.UI;
[CustomEditor (typeof(SubmitInputField), true)]
[CanEditMultipleObjects]
public class SubmitInputFieldEditor : InputFieldEditor
{
SerializedProperty m_KeyboardDoneProperty;
SerializedProperty m_TextComponent;
protected override void OnEnable ()
{
base.OnEnable ();
m_KeyboardDoneProperty = serializedObject.FindProperty ("m_keyboardDone");
m_TextComponent = serializedObject.FindProperty ("m_TextComponent");
}
public override void OnInspectorGUI ()
{
base.OnInspectorGUI ();
EditorGUI.BeginDisabledGroup (m_TextComponent == null || m_TextComponent.objectReferenceValue == null);
EditorGUILayout.Space ();
serializedObject.Update ();
EditorGUILayout.PropertyField (m_KeyboardDoneProperty);
serializedObject.ApplyModifiedProperties ();
EditorGUI.EndDisabledGroup ();
serializedObject.ApplyModifiedProperties ();
}
}
仍然适用于 2019.3!我只需要更改一些内容即可使用 TMP_InputField。