CocosCreator中zip的使用

作者 : 开心源码 本文共1501个字,预计阅读时间需要4分钟 发布时间: 2022-05-13 共299人阅读

写在前头

你在什么时候需要压缩包?

  • 文件太多,只想下载一次
  • 分版本升级内容
  • 数据加密

找个类库咯

能找到类库实现的,就不要自己白费时间了

JSZip

下载dist里面的jszip.min.js扔到CocosCreator里面就好了

准备一个压缩包

随意找点json文件或者者其余文本文件, 当前的压缩包内只能放少量文本文件,少量图片和其余文件在引擎中可能没有解析的方法,也许是我还没找到

压缩包的后缀名改成.bin, 以二进制的方式读取文件,不改的话就直接文本模式读取了

代码实现

Main.ts

const {ccclass, property} = cc._decorator;import JSZip = require('./lib/jszip.min.js')@ccclassexport default class Main extends cc.Component {    @property(cc.Label) resultLabel: cc.Label = null    start () {        cc.loader.loadRes('config', async (err, res) => {            if (err) {                console.log(err)                this.p('加载失败!')            } else {                this.p('加载成功!')                console.log(res)                console.log(res._nativeAsset)                let jsZip = new JSZip()                // console.log(jsZip)                let zip = await jsZip.loadAsync(res._nativeAsset)                if (zip) {                    let file1 = await zip.file('data1.json').async('string')                    let file2 = await zip.file('data2.json').async('string')                    let file3 = await zip.file('data3.json').async('string')                    let file4 = await zip.file('data4.json').async('string')                    let file5 = await zip.file('test.b64.txt').async('string')                    let file6 = await zip.file('dou.png').async('uint8array')                    console.log(file6.byteLength)                    this.p(file1)                    this.p(file2)                    this.p(file3)                    this.p(file4)                    let image = new Image()                    image.src = file5                    image.onload = () => {                        let tex = new cc.Texture2D()                        tex.initWithElement(image)                        let spf = new cc.SpriteFrame(tex)                        let node = new cc.Node()                        let sp = node.addComponent(cc.Sprite)                        sp.spriteFrame = spf                        this.node.addChild(node, -2)                    }                }            }        })    }    p(str: string) {        this.resultLabel.string += `\n${str}`    }}

真机测试

image1.jpg

PS

  • 实现了zip内的数据获取
  • 存储图片的base64编码后的字符串,可以转变为图片
  • 存储图片本人可以取得ArrayBuffer数据,转存可以保存为图片
  • 尚未找到CocosCreator中ArrayBuffer转为cc.Texture2d的方案,如过攻破这个的话,图片加密,数据加密,包体压缩什么的都可以了
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » CocosCreator中zip的使用

发表回复