From 37ee6bf04443ac37c81571f60d43f9ca6e6c21c8 Mon Sep 17 00:00:00 2001 From: Oooocean <41802399@qq.com> Date: Tue, 17 Jan 2023 16:47:35 +0800 Subject: [PATCH] Update InputAdaptation.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改键盘适配示例 --- Design/InputAdaptation.md | 56 ++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/Design/InputAdaptation.md b/Design/InputAdaptation.md index 4e266a3..de80736 100644 --- a/Design/InputAdaptation.md +++ b/Design/InputAdaptation.md @@ -1,25 +1,19 @@ # 输入法适配 在小游戏中Unity游戏唤不起输入法,需要使用WX_SDK中提供的方法来唤起输入法,并做简单的逻辑修改来适配。 -如下以UGUI的Input组件为例,需要给Input 绑定以下脚本: + +详细示例请参考[API Demo](https://github.com/wechat-miniprogram/minigame-unity-webgl-transform/tree/main/Demo/API) + +以UGUI的Input组件为例,需要给Input 绑定以下脚本: ``` public class Inputs : MonoBehaviour, IPointerClickHandler, IPointerExitHandler { public InputField input; + private bool isShowKeyboad = false; public void OnPointerClick(PointerEventData eventData) { Debug.Log("ooooo"); - WX.ShowKeyboard(new ShowKeyboardOption() - { - defaultValue = "xxx", - maxLength = 20, - confirmType = "go" - }); - - //绑定回调 - WX.OnKeyboardConfirm(OnConfirm); - WX.OnKeyboardComplete(OnComplete); - WX.OnKeyboardInput(OnInput); + ShowKeyboad(); } public void OnPointerExit(PointerEventData eventData) @@ -27,11 +21,7 @@ public class Inputs : MonoBehaviour, IPointerClickHandler, IPointerExitHandler // 隐藏输入法 if (!input.isFocused) { - WX.HideKeyboard(new HideKeyboardOption()); - //删除掉相关事件监听 - WX.OffKeyboardInput(OnInput); - WX.OffKeyboardConfirm(OnConfirm); - WX.OffKeyboardComplete(OnComplete); + HideKeyboad(); } } @@ -52,6 +42,7 @@ public class Inputs : MonoBehaviour, IPointerClickHandler, IPointerExitHandler // 输入法confirm回调 Debug.Log("onConfirm"); Debug.Log(v.value); + HideKeyboad(); } public void OnComplete(OnKeyboardInputCallbackResult v) @@ -59,12 +50,39 @@ public class Inputs : MonoBehaviour, IPointerClickHandler, IPointerExitHandler // 输入法complete回调 Debug.Log("OnComplete"); Debug.Log(v.value); + HideKeyboad(); } - - void Start() + private void ShowKeyboad() { + if (!isShowKeyboad) + { + WX.ShowKeyboard(new ShowKeyboardOption() + { + defaultValue = "xxx", + maxLength = 20, + confirmType = "go" + }); + //绑定回调 + WX.OnKeyboardConfirm(OnConfirm); + WX.OnKeyboardComplete(OnComplete); + WX.OnKeyboardInput(OnInput); + isShowKeyboad = true; + } + } + + private void HideKeyboad() + { + if (isShowKeyboad) + { + WX.HideKeyboard(new HideKeyboardOption()); + //删除掉相关事件监听 + WX.OffKeyboardInput(OnInput); + WX.OffKeyboardConfirm(OnConfirm); + WX.OffKeyboardComplete(OnComplete); + isShowKeyboad = false; + } } } -- GitLab