百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

【手把手】Egret如何在微信小游戏创建ComboBox控件

haoteby 2025-05-08 00:56 38 浏览

Egret引擎的官方控件没有ComboBox控件。想创建的话,除了自己重写,还可以调用Egret lab在GitHub上的扩展控件。

在GitHub上搜索egret-game-library,将整个项目以Zip压缩包下载到本地,解压打开。

打开euiextension\ComboBox\libsrc文件夹。如下图:


在Egret整个项目的文件夹外,创建一个名为EgretLibs的文件夹。如果你的项目叫EgretDemo1,路径为D:\Project\EgretProjects\EgretDemo1,那么EgretLibs的路径应为:

D:\Project\EgretProjects\EgretLibs

在EgretLibs文件夹内再创建一个combobox文件夹,然后将上图的所有文件文件夹都复制到这里。


然后修改Egret项目的egretProperties.json文件,在modules节点最后添加如下内容:

{
			"name": "combobox",
			"path": "../EgretLibs/combobox"
}

编译之后,应该在项目的libs/modules文件夹看到生成的combobox文件夹,代表引用成功。

假设游戏的设置页面如下:


那么对应的页面逻辑代码:

    private operationRangeData: any[] = [];
    private operationRangeMaxCB: ComboBox;
		private operationRangeLabel: eui.Label;

    protected childrenCreated(): void {
        super.childrenCreated();

        this.operationRangeData.push({ bg: "itemBg1_png", content: "0" });
        this.operationRangeData.push({ bg: "itemBg2_png", content: "5" });
        this.operationRangeData.push({ bg: "itemBg1_png", content: "10" });
        this.operationRangeData.push({ bg: "itemBg2_png", content: "15" });
        this.operationRangeData.push({ bg: "itemBg1_png", content: "20" });
        this.operationRangeData.push({ bg: "itemBg2_png", content: "30" });
        this.operationRangeData.push({ bg: "itemBg1_png", content: "50" });
        this.operationRangeData.push({ bg: "itemBg2_png", content: "100" });
        
        this.operationRangeMaxCB = this.CreateComboBox(this.operationRangeLabel, this.operationRangeData, 830, 150);
        this.operationRangeMaxCB.name = "operationRangeMaxCB";
        this.operationRangeMaxCB.getTitleLabe().text = this.operationRangeData[0].content;
       
    }

    private CreateComboBox(descLabel: eui.Label, data: any[], positionX: number = 580, boxWidth: number = 400): ComboBox {
        let y = descLabel.y;
        let height = descLabel.height;

        let comboBoxWidth = boxWidth <= 0 ? 400 : boxWidth;
        let comboBoxX = positionX <= 0 ? 580 : positionX;

        let cb: ComboBox = new ComboBox(data);
        this.addChild(cb);
        //1.点击事件
        cb.addEventListener(ComboBox.onClick, this.onClickComboBoxItem, this);
        //2.设置title
        cb.setTitleHeight(height);
        cb.setTitleBackground("titleBackground_png");
        cb.setTitleFontSize(40);
        //3.设置Item
        cb.setItemWidth(comboBoxWidth);
        cb.setItemHeight(height);
        cb.setItemFontSize(descLabel.size - 1);
        //4.设置Item内容文字的对齐方式
        cb.setItemTextAlign("right");

        cb.hide();

        cb.y = y;
        cb.x = comboBoxX;

        return cb;
    }

    private onClickComboBoxItem(event) {
        //getTitleLabe()方法可以获取titleLabel控件。
        var titleLabel = event.currentTarget.getTitleLabe();
        titleLabel.text = event.target._data.content;

        event.currentTarget.hide();
    }



上述这样写,的确可以在Egret Wing debug的时候成功调用ComboBox,但是,如果发布成为微信小程序,则会报ReferenceError或者gameThirdScriptError。

找到Egret项目的scripts/wxgame/wxgame.ts文件,在对应的位置增加以下代码:

    if (filename == "libs/modules/combobox/combobox.js" || filename == 'libs/modules/combobox/combobox.min.js') {
                    content += ";window.ComboBox = ComboBox;"//增加微信小游戏对第三方类库ComboBox的支持
                }

大功告成~~~~~~~~~

相关推荐

Chrome OS 41 用 Freon 取代 X11_chrome os atom

在刚发布的ChromeOS41里,除了常规的Wi-Fi稳定性提升(几乎所有系统的changelog里都会包含这一项)、访客模式壁纸等之外,还存在底层改变。这一更新中Google移除...

苹果iPad Pro再曝光 有望今年六月发布

自进入2015年以后,有关大屏iPad的消息便一直不绝于耳,之前就有不少媒体猜想这款全新的平板电脑将会在三月发布,不过可惜的是我么只在那次发布会上看到了MacBookPro。近日@Ubuntu团队便...

雷卯针对香橙派Orange Pi 5 Max开发板防雷防静电方案

一、应用场景高端平板、边缘计算、人工智能、云计算、AR/VR、智能安防、智能家居、Linux桌面计算机、Linux网络服务器、Android平板、Android游戏机...

Ubuntu Server无法更新问题解决_ubuntu server not found

上周老家的一台运行UbuntuServer的盒子无法连接上了,中秋这两天回来打开,顺手更新一下发现更新报错。提示`E:Releasefileforhttps://mirrors.aliyun...

虚幻引擎5正式版发布:古墓丽影&amp;巫师新作采用、新一代实时渲染

机器之心报道编辑:杜伟、陈萍虚幻引擎5的目标是「助力各种规模的团队在视觉领域和互动领域挑战极限,施展无限潜能」。...

AMD Milan-X双路霄龙7773X平台基准测试曝光 CPU缓存总量超1.5GB

OpenBenchmarking基准测试数据库刚刚曝光了AMDMilan-X双路霄龙7773X平台的跑分成绩,虽然很快就被撤下,但我们还是知晓了高达1.6GB的总CPU缓存。早些时...

ROS机器人建模_ros机器人硬件搭建

...

全网最新的Dify(1.7.2)私有化离线部署教程(ARM架构)

Hello,大家好!近期工作中有涉及到Dify私有化离线部署,特别是针对于一些国产设备。因此特别整理了该教程,实测有效!有需要的小伙伴可以参考下!本文主要针对Dify1.7.2最新版本+国产操作系...

在ubuntu下新建asp.net core项目_创建ubuntu

本文一步步讲述在ubuntu下用visualstudiocode创建asp.netcore项目的过程。step1:环境操作系统:virtualbox下安装的lubuntu。请不要开启“硬件...

在晶晨A311D2处理器上进行Linux硬件视频编码
在晶晨A311D2处理器上进行Linux硬件视频编码

在KhadasVIM4AmogicA311D2SBC上,我更多的时间是在使用Ubuntu22.04。它的总体性能还不错,只不过缺少3D图形加速和硬件视...

2025-08-26 17:22 haoteby

Nacos3.0重磅来袭!全面拥抱AI,单机及集群模式安装详细教程!

之前和大家分享过JDK17的多版本管理及详细安装过程,然后在项目升级完jdk17后又发现之前的注册和配置中心nacos又用不了,原因是之前的nacos1.3版本的,版本太老了,已经无法适配当前新的JD...

电影质量级渲染来了!虚幻引擎5.3正式发布:已开放下载

快科技9月8日消息,日前,Unrealengine正式发布了虚幻引擎5.3,带来了大量全方位的改进。...

2025如何选购办公电脑?极摩客mini主机英特尔系列选购指南

当下,迷你主机的性能越来越强,品类也越来越多。但是CPU是不变的,基本都是AMD和英特尔的。有一个小伙伴在评论区提问,我应该如何在众多机器中选购一台符合自己的迷你主机呢?那今天我们优先把我们的系列,分...

ubuntu 20.04+RTX4060 Ti+CUDA 11.7+cudnn

ububtu添加国内源sudocp/etc/apt/sources.list/etc/apt/sources.list.backupsudovim/etc/apt/sources.lis...

Linux Mint 18将重新基于Ubuntu 16.04 带来更好硬件支持

项目负责人ClementLefebvre在本月6日披露了关于LinuxMint18“Sarah”操作系统的大量信息,包括带来全新扁平化体验的Mint-Y主题。而现在,这款将于年底之前上线的操作...