• Unity微信小游戏无法调起输入框


    最近在开发一款微信小游戏,有个微信修改名字的功能。突然发现发存微信小游戏后,输入框无法调起键盘。
    后发现需要发布为WebGL后需要调用微信的一些接口才能调起,这边做个记录。

    using UnityEngine;
    using UnityEngine.UI;
    using WeChatWASM;
    public class KeyboardHandle
    {
        #region 私有变量
        //------------------------------------------------------------------------------------
        private InputField input;
        private bool isShowKeyboard = false;
        //------------------------------------------------------------------------------------
        #endregion
        #region 公有变量
        //------------------------------------------------------------------------------------
        //------------------------------------------------------------------------------------
        #endregion
        #region 私有方法
        //------------------------------------------------------------------------------------
        public void ShowKeyboard(InputField target) { __ShowKeyboard(target); }    //打开键盘
        public void HideKeyboard() { __HideKeyboard(); }                //隐藏键盘
        //------------------------------------------------------------------------------------
        #endregion
        #region 公有方法
        //------------------------------------------------------------------------------------
        public void OnInput(OnKeyboardInputListenerResult v)
        {
            Debug.Log("onInput");
            Debug.Log(v.value);
            if (input.isFocused)
            {
                input.text = v.value;
            }
        }
        //------------------------------------------------------------------------------------
        public void OnConfirm(OnKeyboardInputListenerResult v)
        {
            // 输入法confirm回调
            Debug.Log("onConfirm");
            Debug.Log(v.value);
            HideKeyboard();
        }
        //------------------------------------------------------------------------------------
        public void OnComplete(OnKeyboardInputListenerResult v)
        {
            // 输入法complete回调
            Debug.Log("OnComplete");
            Debug.Log(v.value);
            HideKeyboard();
        }
        //------------------------------------------------------------------------------------
        private void __ShowKeyboard(InputField target)
        {
    #if !UNITY_EDITOR && UNITY_WEBGL
                if (target != null)
                {
                    input = target;
                    if (!isShowKeyboard)
                    {
                        WX.ShowKeyboard(new ShowKeyboardOption()
                        {
                            defaultValue = target.text,
                            maxLength = 20,
                            confirmType = "go"
                        });
                        //绑定回调
                        WX.OnKeyboardConfirm(OnConfirm);
                        WX.OnKeyboardComplete(OnComplete);
                        WX.OnKeyboardInput(OnInput);
                        isShowKeyboard = true;
                    }
                }
    #endif
        }
        //------------------------------------------------------------------------------------
        private void __HideKeyboard()
        {
    #if !UNITY_EDITOR && UNITY_WEBGL
                if (isShowKeyboard)
                {
                    WX.HideKeyboard(new HideKeyboardOption());
                    //删除掉相关事件监听
                    WX.OffKeyboardInput(OnInput);
                    WX.OffKeyboardConfirm(OnConfirm);
                    WX.OffKeyboardComplete(OnComplete);
                    isShowKeyboard = false;
                }
    #endif
        }
        //------------------------------------------------------------------------------------
        #endregion
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
  • 相关阅读:
    2022-08-25 AndroidR 默认赋予app权限,不弹出权限申请窗口
    分别用Python和Go实现对文件夹及其子文件夹里的文件进行批量重命名
    使用软路由(openWrt)安装openVPN搭建局域网连接
    大数据开发工程师面试题
    浅谈用匈牙利算法寻找二分图的最大匹配
    .Net开发——EFCore
    零基础爬虫之http协议
    基于深度学习的双目重建
    技术文档工具『Writerside』抢鲜体验
    【数学建模】混合整数规划MIP(Python+Gurobi代码实现)
  • 原文地址:https://blog.csdn.net/ww1351646544/article/details/132742618