1.客户端代码如下:点击按钮调用onClickButton方法触发检查更新的系统消息
cc.Class({
extends: cc.Component,
properties: {
},
onClickButton() {
cc.systemEvent.emit("check_update");
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
start() {
cc.systemEvent.on("is_new", () => {
cc.director.loadScene("main");
});
},
// update (dt) {},
});
热更新的源码可以在creator官方文档中介绍热更新的文章中找到github地址
热更新源码中的onLoad监听检查更新的系统事件,当触发此系统消息时会自动调用checkUpdate方法
cc.systemEvent.on("check_update", () => {
this.checkUpdate();
});
源码中有设置检查更新后的回调函数checkCb,如果是最新版本就会触发‘is_new’系统消息,就会自动跳转界面,如果检测到有更新的版本就会调用hotUpdate函数,注意要把回调函数清空,因为后面还会设置更新完成后的回调函数
checkCb(event) {
let flag = 0;
switch (event.getEventCode()) {
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
break;
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
cc.systemEvent.emit("is_new"); // 已经是最新版本
break;
case jsb.EventAssetsManager.NEW_VERSION_FOUND:
flag = 1;
break;
default:
return;
}
this._updating = false;
this._am.setEventCallback(null);
this._checkListener = null;
if (flag) {
this.hotUpdate();
}
},
更新完成后会调用回调函数,重新启动游戏
updateCb(event) {
var needRestart = false;
var failed = false;
switch (event.getEventCode()) {
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
cc.systemEvent.emit("is_new");
failed = true;
break;
case jsb.EventAssetsManager.UPDATE_FINISHED:
needRestart = true;
break;
case jsb.EventAssetsManager.UPDATE_FAILED:
this._updating = false;
this._canRetry = true;
break;
default:
break;
}
if (failed) {
this._am.setEventCallback(null);
this._updateListener = null;
this._updating = false;
}
if (needRestart) {
this._am.setEventCallback(null);
this._updateListener = null;
var searchPaths = jsb.fileUtils.getSearchPaths();
var newPaths = this._am.getLocalManifest().getSearchPaths();
console.log(JSON.stringify(newPaths));
Array.prototype.unshift.apply(searchPaths, newPaths);
cc.sys.localStorage.setItem('HotUpdateSearchPaths', JSON.stringify(searchPaths));
jsb.fileUtils.setSearchPaths(searchPaths);
cc.audioEngine.stopAll();
cc.game.restart();
}
},
2.服务器
let express = require("express");
let app = express();
app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
next();
});
app.use(express.static("static"));
app.listen(7777);
3.asset目录下创建一个resources,在其目录下创建一个存放两个.manifest文件的目录
4.先构建项目生成build项目资源文件目录
5.使用热更新工具,资源服务器url需要加上服务器监听的端口和static下的目录名
(1)例如http://192.168.0.104:7777/hot
6.生成两个配置文件 project.manifest和version.manifest
脚本关联project再构建发布初始版本
7.用热更新工具生成的高版本包中的资源和配置文件放在服务器static目录下