HotUpdatePanel.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import HotUpdate, { HotOptions } from "../../../mk/sdk/hot/HotUpdate";
  2. import { UpdateState } from "../../data/AppData";
  3. const { ccclass, property } = cc._decorator;
  4. @ccclass
  5. export default class HotUpdatePanel extends cc.Component {
  6. @property({ displayName: 'project.manifest', type: cc.Asset, })
  7. manifest: cc.Asset = null;
  8. // @property({ displayName: '版本号', type: cc.Label, })
  9. // versionLabel: cc.Label = null;
  10. // @property({ displayName: '热更新进度条', type: cc.ProgressBar })
  11. // updateProgress: cc.ProgressBar = null;
  12. // @property({ displayName: '消息提示', type: cc.Label })
  13. // tipsLabel: cc.Label = null;
  14. // @property({ displayName: '添加节点', type: cc.Node })
  15. // addNode: cc.Node = null;
  16. onLoad() {
  17. this.initView();
  18. let options = new HotOptions();
  19. options.OnVersionInfo = (data) => {
  20. let { local, server } = data;
  21. // this.versionLabel.string = `本地版本号:${local}, 服务器版本号:${server}`;
  22. };
  23. options.OnUpdateProgress = (event: jsb.EventAssetsManager) => {
  24. let bytes = event.getDownloadedBytes() + '/' + event.getTotalBytes();
  25. let files = event.getDownloadedFiles() + '/' + event.getTotalFiles();
  26. let file = event.getPercentByFile().toFixed(2);
  27. let byte = event.getPercent().toFixed(2);
  28. let msg = event.getMessage();
  29. gData.appData.updateState = UpdateState.IsUpdate;
  30. gData.appData.updatePercent = parseFloat(file);
  31. console.log('[update]: 进度=' + file);
  32. };
  33. options.OnNeedToUpdate = (data) => {
  34. HotUpdate.hotUpdate();
  35. };
  36. options.OnNoNeedToUpdate = () => {
  37. this.enterGame();
  38. };
  39. options.OnUpdateFailed = () => {
  40. // this.tipsLabel.string = '更新失败';
  41. console.log('热更新失败');
  42. setTimeout(() => {
  43. HotUpdate.checkUpdate();
  44. }, 2000);
  45. };
  46. options.OnUpdateSucceed = () => {
  47. // this.tipsLabel.string = '更新成功';
  48. console.log('更新成功');
  49. setTimeout(() => {
  50. cc.audioEngine.stopAll();
  51. cc.game.restart();
  52. }, 500);
  53. };
  54. HotUpdate.init(this.manifest, options);
  55. }
  56. /** */
  57. start() {
  58. }
  59. initView() {
  60. // this.tipsLabel.string = '';
  61. // this.versionLabel.string = '';
  62. // this.updateProgress.progress = 0;
  63. // this.addNode.destroyAllChildren();
  64. }
  65. // 检查更新
  66. checkHotUpdate() {
  67. if (cc.sys.isNative) {
  68. if (this.manifest) {
  69. // this.tipsLabel.string = '正在获取版本...';
  70. HotUpdate.checkUpdate();
  71. }
  72. } else {
  73. console.log('web 平台不需要热更新');
  74. this.enterGame();
  75. }
  76. }
  77. enterGame() {
  78. console.log('进入游戏成功');
  79. gData.appData.updateState = UpdateState.UpdateFinish;
  80. }
  81. }