如何检测软键盘Unity3d中的"完成"按钮

dev*_*sih 2 c# android unity-game-engine soft-keyboard

我在我的Android应用程序中使用一个输入字段来获取一个字符串当我输入一个字符串时弹出一个软键盘但是现在我想在用户按下软键盘中的"完成"按钮时调用一个函数我该怎么办?
Unity3d 4.6.2f1
完成密钥

wha*_*011 6

我发现的最好方法是继承InputField.您可以在bitbucket上查看UnityUI的源代码.在该子类中,您可以访问受保护的m_keyboard字段并检查是否已按下并且未取消,这将为您提供所需的结果.使用EventSystem的"提交"无法正常工作.将它集成到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 ();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑/ 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 ();
    }
}
Run Code Online (Sandbox Code Playgroud)