Cocos Creator spine打包成zip下载并解压
业务上的小需求,相关的spine想压缩成一个zip方便管理和外包,也想着游戏里也这么用,但着急上线,就需要网络下载,常规办法是spine打包成3个文件,之后分别下载,代码如下
let comp = this.getComponent('sp.Skeleton') as sp.Skeleton;
let image = "http://localhost/download/spineres/1/1.png";
let ske = "http://localhost/download/spineres/1/1.json";
let atlas = "http://localhost/download/spineres/1/1.atlas";
assetManager.loadAny([{ url: atlas, ext: '.txt' }, { url: ske, ext: '.txt' }], (error, assets) => {
assetManager.loadRemote(image, (error, texture: Texture2D) => {
let asset = new sp.SkeletonData();
asset.skeletonJson = assets[1];
asset.atlasText = assets[0];
asset.textures = [texture];
asset.textureNames = ['1.png'];
skeleton.skeletonData = asset;
});
});
方法很常规,但有个管理问题,就是可能在文件迁移或者管理的时候不明晰,容易混(其实还是运营和后端对自己不关心的东西不在意觉得烦而已),于是乎压缩成zip就相当方便了
cc.assetManager.loadRemote(this._sub1Data.gameAnimationURL, (err, zipBin: any) => {
if (err) return;
if (this && this.isValid) {
JSZip.loadAsync(zipBin._buffer).then((zip) => {
zip.file(spineName + ".png").async('uint8array').then((data3) => {
zip.file(spineName + ".atlas").async("text").then((data2) => {
zip.file(spineName + ".json").async("text").then((data1) => {
let asset = new sp.SkeletonData();
asset.skeletonJson = data1;
asset.atlasText = data2;
asset['_uuid'] = '43534'
let texture = new cc.Texture2D();
const UPNG = window['UPNG'];
let png = UPNG.decode(data3.buffer);
let flag = texture.initWithData(png.data,cc.Texture2D.PixelFormat.RGBA8888,png.width, png.height);
console.log("flag_"+flag);
texture.handleLoadedTexture();
asset['textureNames'].push(spineName + ".png");
asset.textures.push(texture);
spine.skeletonData = asset;
imageNode.active = false;
spine.node.active = true;
spine.setAnimation(0, 'animation', true);
})
});
})
});
}
})
上面代码最核心就是jszip解压,upng图片解压成原图像素信息,之后加载spine的skeletonData。
Creator版本2.4.12,jszip随便下载一份就可以,upng也随意能下载到。
随手记录,没啥高深技术,就是遇到业务需求问题就随手记录。