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

你可能感兴趣的:(unity,微信,小键盘,微信转入框)