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; // 更新键盘显示状态
    }
}
相关推荐
绝缘体11 天前
如何使用外卖霸王餐api接口?
大数据·搜索引擎·微信·pygame
新时代牛马1 天前
CANopen 协议详解
linux·微信
小贺儿开发1 天前
Unity3D 智慧城市管理平台
数据库·人工智能·unity·智慧城市·数据可视化
June bug2 天前
【领域知识】休闲游戏一次发版全流程:Google Play + Apple App Store
unity
星夜泊客2 天前
C# 基础:为什么类可以在静态方法中创建自己的实例?
开发语言·经验分享·笔记·unity·c#·游戏引擎
dzj20212 天前
PointerEnter、PointerExit、PointerDown、PointerUp——鼠标点击物体,则开始旋转,鼠标离开或者松开物体,则停止旋转
unity·pointerdown·pointerup
心前阳光2 天前
Unity 模拟父子关系
android·unity·游戏引擎
在路上看风景2 天前
26. Mipmap
unity
陈思杰系统思考Jason2 天前
系统问题误作态度问题
百度·微信·微信公众平台·新浪微博·微信开放平台
咸鱼永不翻身2 天前
Unity视频资源压缩详解
unity·游戏引擎·音视频