HotUpdateUI.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /** @format */
  2. import {Log} from '../utils/LogUtils'
  3. import {BaseUI} from './BaseUI'
  4. import {Mgr} from '../GameControl'
  5. import HotUpdate, {HotOptions} from '../hotupdate/hotupdate'
  6. const {ccclass, property} = cc._decorator
  7. @ccclass
  8. export default class HotUpdateUI extends BaseUI {
  9. @property({displayName: 'project.manifest', type: cc.Asset})
  10. manifest: cc.Asset = null
  11. @property({displayName: '版本号', type: cc.Label})
  12. versionLabel: cc.Label = null
  13. @property({displayName: '热更新进度条', type: cc.ProgressBar})
  14. updateProgress: cc.ProgressBar = null
  15. @property({displayName: '消息提示', type: cc.Label})
  16. tipsLabel: cc.Label = null
  17. localVersion: string = ''
  18. onLoad() {
  19. this._initView()
  20. let options = new HotOptions()
  21. options.OnVersionInfo = data => {
  22. let {local, server} = data
  23. this.versionLabel.string = `本地版本号:${local}, 服务器版本号:${server}`
  24. this.localVersion = local
  25. }
  26. options.OnUpdateProgress = (event: jsb.EventAssetsManager) => {
  27. let bytes = event.getDownloadedBytes() + '/' + event.getTotalBytes()
  28. let files = event.getDownloadedFiles() + '/' + event.getTotalFiles()
  29. let file = event.getPercentByFile().toFixed(2)
  30. let byte = event.getPercent().toFixed(2)
  31. let msg = event.getMessage()
  32. console.log('[update]: 进度=' + file)
  33. this.updateProgress.progress = parseFloat(file)
  34. this.tipsLabel.string = '正在更新中,请耐心等待'
  35. console.log(msg)
  36. }
  37. options.OnNeedToUpdate = data => {
  38. this.tipsLabel.string = '检测到新版本,开始更新'
  39. HotUpdate.hotUpdate()
  40. }
  41. options.OnNoNeedToUpdate = () => {
  42. this.closeAndTip(`当前版本${this.localVersion}为最新版本,暂无更新`)
  43. }
  44. options.OnUpdateFailed = () => {
  45. console.log('热更新失败')
  46. this.closeAndTip('热更新失败')
  47. }
  48. options.OnUpdateSucceed = () => {
  49. this.tipsLabel.string = '更新成功'
  50. console.log('更新成功')
  51. cc.audioEngine.stopAll()
  52. cc.game.restart()
  53. }
  54. HotUpdate.init(this.manifest, options)
  55. }
  56. protected onEnable() {
  57. console.log('热更新代码日志')
  58. this.startCheckUpdate()
  59. }
  60. _initView() {
  61. this.tipsLabel.string = ''
  62. this.versionLabel.string = ''
  63. this.updateProgress.progress = 0
  64. }
  65. // 检查更新
  66. startCheckUpdate() {
  67. if (cc.sys.isNative) {
  68. if (this.manifest) {
  69. this.tipsLabel.string = '正在获取版本...'
  70. HotUpdate.checkUpdate()
  71. }
  72. } else {
  73. console.log('web 平台不需要热更新')
  74. }
  75. }
  76. closeAndTip(tip: string) {
  77. this.hide()
  78. Mgr.ui.tip(tip)
  79. }
  80. }