cocosCreator导出的web工程加载本地图片
cocosCreator版本:3.7.2
在 Web 平台开发 Cocos Creator 游戏或应用时,有时候我们希望让用户上传一张本地图片用于展示、头像设置等。
但 Cocos 本身不提供原生文件选择器,这里我们借助 HTML
原生的 <input type="file">
,再结合 TypeScript
实现完整的逻辑。
公开链接https://lengmo714.top/76698fe7.html
创建一个隐藏的图片选择器
创建一个隐藏的图片选择器,用于选择图片,并监听选择文件事件
onLoad() {this.creatorImagePicker();}createImagePicker() {this.inputElement = document.createElement('input');this.inputElement.type = 'file';this.inputElement.accept = 'image/*'; // 仅允许选择图片this.inputElement.style.display = 'none'; // 隐藏 input 元素document.body.appendChild(this.inputElement);this.inputElement.addEventListener('change', this.handleFileChange