CocosCreator系列——热更新超详细教程—2.2.1版本
新版本的热更新网上找了一些教程,大都大同小异,2.X新版本的更是少得可怜,最后运行的结果都是——运行不了……下载了官方案例,又经过两天的折腾,终于算是搞定了。
前期准备工作:首先看一下我另外一篇文章,先搭建一个简单的本地服务器.
1.新建一个checkUpdate场景,一个提示信息label,两个进度条和两个下载进度文字的显示label,内容如下:
然后编辑hotUpdate.js代码:
var UpdatePanel = require('../UI/UpdatePanel');
cc.Class({
extends: cc.Component,
properties: {
panel: UpdatePanel,
manifestUrl: {
type: cc.Asset,
default: null
},
updateUI: cc.Node,
_updating: false,
_canRetry: false,
_storagePath: ''
},
onUpdateClick() {
this.hotUpdate();
this.updateUI.active = false;
},
onCancelClick() {
cc.director.pause();
cc.audioEngine.stopAll();
if (cc.director.isPaused()) {
cc.game.end();
}
},
checkCb: function (event) {
cc.log('Code: ' + event.getEventCode());
let isUpdate = false;
switch (event.getEventCode()) {
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
this.panel.info.string = "未找到本地清单文件,已跳过热更新.";
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
this.panel.info.string = "无法下载清单文件,已跳过热更新.";
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
this.panel.info.string = "已经是最新远程版本的最新版本.";
break;
case jsb.EventAssetsManager.NEW_VERSION_FOUND:
this.panel.info.string = '找到新版本,请尝试更新.';
isUpdate = true ;
this.panel.fileProgress.progress = 0;
this.panel.byteProgress.progress = 0;
break;
default:
return;
}
this._am.setEventCallback(null);
this._checkListener = null;
this._updating = false;
if (isUpdate) {
this.updateUI.active = true;
}
},
updateCb: function (event) {
var needRestart = false;
var failed = false;
switch