gpt4 book ai didi

c# - 如何在软键盘Unity3d中检测 "done"按钮

转载 作者:太空狗 更新时间:2023-10-29 16:15:23 29 4
gpt4 key购买 nike

我在我的 android 应用程序中使用 InputField 来获取一个字符串,当我输入一个字符串时会弹出一个软键盘,但现在我想在用户按下软键盘中的“完成”按钮时调用一个函数,我该怎么做?
Unity3d 4.6.2f1
done key

最佳答案

我发现的最佳方法是将 InputField 子类化。您可以在 bitbucket 上查看 UnityUI 的源代码。在该子类中,您可以访问 protected m_keyboard 字段并检查完成是否已按下且未取消,这将为您提供所需的结果。使用 EventSystem 的“提交”无法正常工作。如果将它集成到 Unity EventSystem 中,效果会更好。

是这样的:提交输入字段.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 ();
}
}

关于c# - 如何在软键盘Unity3d中检测 "done"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29670276/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com