C# “如何检测”;“完成”;软键盘Unity3d中的按钮

C# “如何检测”;“完成”;软键盘Unity3d中的按钮,c#,android,unity3d,soft-keyboard,C#,Android,Unity3d,Soft Keyboard,我在android应用程序中使用输入字段来获取字符串输入字符串时,软键盘会弹出,但现在我想在用户按下软键盘中的“完成”按钮时调用一个函数。我如何才能做到这一点? Unity3d 4.6.2f1 您应该能够通过以下方法实现同样的效果: function Update () { Event e = Event.currrent; if (e.type == EventType.keyDown && e.keyCode == KeyCode.Return

我在android应用程序中使用输入字段来获取字符串输入字符串时,软键盘会弹出,但现在我想在用户按下软键盘中的“完成”按钮时调用一个函数。我如何才能做到这一点?
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_键盘字段,并检查是否按了done而未取消,这将为您提供所需的结果。使用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 ();
        }
    }
}
编辑器/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,我只需更改一些内容。

什么是“e”,我可以访问谁?我在unity3d中使用c#
e
是事件的变量。您可以使用此
Event e=Event.current当我使用你的代码时,我得到大量的空指针异常。但与您的代码类似,我使用了
Input.GetKeyDown(KeyCode.Return)
,现在它在编辑器中工作,但在android设备中仍然不工作。我找到了一种解决方法,我发布了我的解决方案。你成功了!令人尴尬的是,Unity没有这个非常常见的任务,它是现成解决的!在iOS上测试和工作。统一5.4.1f1