Unity导出微信小游戏后无法调起移动端输入框

参考官方demo 参考demo https://gitee.com/wechat-minigame/minigame-unity-webgl-transform/tree/main/Demo/API_V2

里面有对输入文本框适配的处理方法,还有一些其他功能展示

1 使用Unity打开/Demo/API目录,API示例开发版本为Unity 2022.3.14f1。

2 若Unity为2020及以前(如2019)版本,则需要在/Assets/Scripts/Editor/PreBuildProcessing.cs中配置Python环境变量。

3 点击"工具栏------微信小游戏------转换小游戏",填写自己的游戏AppID并设置导出路径,以及设置其他导出选项,之后点击"生成并转换"。

使用微信开发者工具打开导出路径/minigame。

找到脚本 WXInputFieldTmpAdapter,复制到自己项目中
在文本输入组件上面挂载改脚本即可调起输入框

csharp 复制代码
using UnityEngine;
using WeChatWASM;
using TMPro;
using UnityEngine.EventSystems;

// 要求该组件必须附加 TMP_InputField 组件
[RequireComponent(typeof(TMP_InputField))]
public class WXInputFieldTmpAdapter : MonoBehaviour, IPointerClickHandler, IPointerExitHandler
{
    private TMP_InputField _inputField; // 存储 TMP_InputField 组件的引用
    private bool _isShowKeyboard = false; // 标记键盘是否显示

    private void Start()
    {
        // 获取挂载在同一游戏对象上的 TMP_InputField 组件
        _inputField = GetComponent<TMP_InputField>();
    }

    // 当指针点击该组件时调用
    public void OnPointerClick(PointerEventData eventData)
    {
        ShowKeyboard(); // 显示键盘
    }

    // 当指针离开该组件时调用
    public void OnPointerExit(PointerEventData eventData)
    {
        // 如果 TMP_InputField 没有被聚焦,则隐藏键盘
        if (!_inputField.isFocused)
        {
            HideKeyboard();
        }
    }

    // 输入法输入回调
    private void OnInput(OnKeyboardInputListenerResult v)
    {
        // 如果 TMP_InputField 被聚焦,则将输入值赋给 TMP_InputField
        if (_inputField.isFocused)
        {
            _inputField.text = v.value;
        }
    }

    // 输入法确认回调
    private void OnConfirm(OnKeyboardInputListenerResult v)
    {
        HideKeyboard(); // 隐藏键盘
    }

    // 输入法完成回调
    private void OnComplete(OnKeyboardInputListenerResult v)
    {
        HideKeyboard(); // 隐藏键盘
    }

    // 显示键盘的方法
    private void ShowKeyboard()
    {
        // 如果键盘已经显示,则直接返回
        if (_isShowKeyboard) return;

        // 调用 WeChat API 显示键盘
        WX.ShowKeyboard(new ShowKeyboardOption()
        {
            defaultValue = _inputField.text,//传入当前文本作为默认值
            maxLength = 20, // 最大输入长度
            confirmType = "go" // 确认按钮类型
        });

        // 绑定键盘事件回调
        WX.OnKeyboardConfirm(this.OnConfirm);
        WX.OnKeyboardComplete(this.OnComplete);
        WX.OnKeyboardInput(this.OnInput);
        _isShowKeyboard = true; // 更新键盘显示状态
    }

    // 隐藏键盘的方法
    private void HideKeyboard()
    {
        // 如果键盘未显示,则直接返回
        if (!_isShowKeyboard) return;

        // 调用 WeChat API 隐藏键盘
        WX.HideKeyboard(new HideKeyboardOption());
        // 移除事件监听
        WX.OffKeyboardInput(this.OnInput);
        WX.OffKeyboardConfirm(this.OnConfirm);
        WX.OffKeyboardComplete(this.OnComplete);
        _isShowKeyboard = false; // 更新键盘显示状态
    }
}
相关推荐
狂人开飞机1 小时前
27、实战物理益智游戏
游戏·游戏引擎·godot
本人手速666+1 小时前
企业微信二次开发中的事件驱动架构:如何把外部事件变成内部流程
微信·自动化·企业微信·个人开发·微信开放平台
xiezhr1 小时前
微信里多了个[小微],可以帮你看朋友圈、发消息、点外卖了
微信·aigc·agent
懂软件的胡子个哥1 小时前
微信工单系统如何基于 WechatApi 做消息分流和状态流转
运维·分布式·微信·架构·企业微信
小小数媒成员16 小时前
如何优化代码适应托管内存?
游戏·unity·游戏引擎
狂人开飞机1 天前
21、游戏架构设计模式
游戏·游戏引擎·godot
行者全栈架构师2 天前
WorkBuddy 实战:50 份简历 30 分钟筛完,还能自动出评估报告
人工智能·算法·微信
新的瑞拉公主2 天前
Unity游戏发布微信小游戏:从构建到提审
unity·webgl·微信小游戏·开放数据域
狂人开飞机3 天前
14、游戏手感与视觉打磨
游戏·游戏引擎·godot