HotUpdatePanel.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. cc.audioEngine.stopAll();
  50. cc.game.restart();
  51. };
  52. HotUpdate.init(this.manifest, options);
  53. }
  54. /** */
  55. start() {
  56. }
  57. initView() {
  58. // this.tipsLabel.string = '';
  59. // this.versionLabel.string = '';
  60. // this.updateProgress.progress = 0;
  61. // this.addNode.destroyAllChildren();
  62. }
  63. // 检查更新
  64. checkHotUpdate() {
  65. if (cc.sys.isNative) {
  66. if (this.manifest) {
  67. // this.tipsLabel.string = '正在获取版本...';
  68. HotUpdate.checkUpdate();
  69. }
  70. } else {
  71. console.log('web 平台不需要热更新');
  72. this.enterGame();
  73. }
  74. }
  75. enterGame() {
  76. console.log('进入游戏成功');
  77. gData.appData.updateState = UpdateState.UpdateFinish;
  78. }
  79. }