/** @format */ import {Log} from '../utils/LogUtils' import {BaseUI} from './BaseUI' import {Mgr} from '../GameControl' import HotUpdate, {HotOptions} from '../hotupdate/hotupdate' const {ccclass, property} = cc._decorator @ccclass export default class HotUpdateUI extends BaseUI { @property({displayName: 'project.manifest', type: cc.Asset}) manifest: cc.Asset = null @property({displayName: '版本号', type: cc.Label}) versionLabel: cc.Label = null @property({displayName: '热更新进度条', type: cc.ProgressBar}) updateProgress: cc.ProgressBar = null @property({displayName: '消息提示', type: cc.Label}) tipsLabel: cc.Label = null localVersion: string = '' onLoad() { this._initView() let options = new HotOptions() options.OnVersionInfo = data => { let {local, server} = data this.versionLabel.string = `本地版本号:${local}, 服务器版本号:${server}` this.localVersion = local } options.OnUpdateProgress = (event: jsb.EventAssetsManager) => { let bytes = event.getDownloadedBytes() + '/' + event.getTotalBytes() let files = event.getDownloadedFiles() + '/' + event.getTotalFiles() let file = event.getPercentByFile().toFixed(2) let byte = event.getPercent().toFixed(2) let msg = event.getMessage() console.log('[update]: 进度=' + file) this.updateProgress.progress = parseFloat(file) this.tipsLabel.string = '正在更新中,请耐心等待' console.log(msg) } options.OnNeedToUpdate = data => { this.tipsLabel.string = '检测到新版本,开始更新' HotUpdate.hotUpdate() } options.OnNoNeedToUpdate = () => { this.closeAndTip(`当前版本${this.localVersion}为最新版本,暂无更新`) } options.OnUpdateFailed = () => { console.log('热更新失败') this.closeAndTip('热更新失败') } options.OnUpdateSucceed = () => { this.tipsLabel.string = '更新成功' console.log('更新成功') cc.audioEngine.stopAll() cc.game.restart() } HotUpdate.init(this.manifest, options) } protected onEnable() { console.log('热更新代码日志') this.startCheckUpdate() } _initView() { this.tipsLabel.string = '' this.versionLabel.string = '' this.updateProgress.progress = 0 } // 检查更新 startCheckUpdate() { if (cc.sys.isNative) { if (this.manifest) { this.tipsLabel.string = '正在获取版本...' HotUpdate.checkUpdate() } } else { console.log('web 平台不需要热更新') } } closeAndTip(tip: string) { this.hide() Mgr.ui.tip(tip) } }