1.将要打包的的资源和layaairfiles.txt文件一起打成zip.
2.预加载阶段,先将zip包通过downfile下载并解压到缓存目录.
3.设置baseurl,开始预加载所有资源.
4.下面为node.js编写的自动生成layaairfiles.txt的脚本
var fs = require("fs"); var cacheData = ""; var tmp = {}; var totalSize = 0; var nowTime = Date.now(); var BaseUrl = "https://0.0.3/"; var BasePath = "./bin/"; var fileList = fs.readdirSync(BasePath); tmp["fileUsedSize"] = totalSize; for(var i=0;i<fileList.length;i++){ var file = fileList[i]; var stats = fs.statSync(BasePath+file); if(stats.isDirectory()){ readFile(BasePath+file+"/",file+"/"); }else{ totalSize += stats.size; tmp[BaseUrl+file] = {}; var data = tmp[BaseUrl+file]; data["md5"] = prePath+file; data["readyUrl"] = BaseUrl+file; data["size"] = stats.size; data["times"] = nowTime; var arr = file.split('.'); if(arr[1] == "atlas"){ data["encoding"] = "utf8"; }else{ data["encoding"] = ""; } } } tmp["fileUsedSize"] = totalSize; var str = JSON.stringify(tmp); fs.writeFileSync('./layaairfiles.txt',str); function readFile(readurl,prePath){ var fileList = fs.readdirSync(readurl); for(var i=0;i<fileList.length;i++){ var file = fileList[i]; var stats = fs.statSync(readurl+file); if(stats.isDirectory()){ readFile(readurl+"/"+file+"/",prePath+file+"/"); }else{ totalSize += stats.size; tmp[BaseUrl+prePath+file] = {}; var data = tmp[BaseUrl+prePath+file]; data["md5"] = prePath+file; data["readyUrl"] = BaseUrl+prePath+file; data["size"] = stats.size; data["times"] = nowTime; var arr = file.split('.'); if(arr[1] == "atlas"){ data["encoding"] = "utf8"; }else{ data["encoding"] = ""; } } } }
5.将下载下来的zip文件解压到缓存目录
Laya.MiniAdpter.removeAll(); wx.getFileSystemManager().unzip({ zipFilePath: res.tempFilePath, targetPath: laya.wx.mini.MiniFileMgr.fileNativeDir, success: () => { this.unzipSuccess(); }, fail: (res) => { this.unzipFail(res); } });