如何检测"完成"按钮softkeyboard Unity3d [英] How to detect "done" Button in softkeyboard Unity3d

查看:379
本文介绍了如何检测"完成"按钮softkeyboard Unity3d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用InputField在我的Andr​​oid应用程序得到一个字符串的软键盘的弹出当我进入了一个字符串,但现在我想调用一个函数,当用户preSS完成在softkeyboard按钮,我怎么能这样做吗?结果
Unity3d 4.6.2f1结果

I use a InputField in my android app to get a string a soft keyboard pops up when i'm entering a string but now i want to call a function when user press "done" button in softkeyboard how can i do that?
Unity3d 4.6.2f1

推荐答案

我找到了最好的办法是继承InputField。你可以看看的信号源UnityUI上到位桶。在该子类可以访问受保护的m_keyboard领域,检查是否所作所为是pressed,而不是取消,会给​​你想要的结果。使用提交的的EventSystem无法正常工作。
即使你将它集成到统一的EventSystem更好。

the best way i found is to subclass InputField. You can look into the source for the UnityUI on bitbucket. In that subclass you can access the protected m_keyboard field and check whether done was pressed AND not canceled, that will give you the desired result. Using "submit" of the EventSystem doesn't work properly. Even nicer when you integrate it into the Unity EventSystem.

事情是这样的:
SubmitInputField.cs

Something like this: 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

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 ();
    }
}

这篇关于如何检测"完成"按钮softkeyboard Unity3d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆