SubPackageLoader.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { _decorator, Component, Node, sys } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('SubPackageLoader')
  4. export class SubPackageLoader {
  5. static load(packages: string[], progressCallBack: Function, completeCallBack: Function, errorCallback?: Function): void {
  6. console.log("平台:" + sys.browserType);
  7. // switch (sys.browserType) {
  8. // case sys.BROWSER_TYPE_WECHAT:
  9. this.__loadWX(packages, progressCallBack, completeCallBack, errorCallback);
  10. // break;
  11. // default:
  12. // break;
  13. // }
  14. }
  15. private static loadSubpackage(index: number, packages: string[], progressCallBack: Function, completeCallBack: Function, errorCallback?: Function): void {
  16. let loadTask: any;
  17. let wx: any = window["wx"];
  18. let packageName: string = packages[index];
  19. console.log("加载分包:", packageName)
  20. index++;
  21. loadTask = wx.loadSubpackage({
  22. name: packageName,
  23. success: (res) => {
  24. if (index < packages.length) {
  25. this.loadSubpackage(index, packages, progressCallBack, completeCallBack, errorCallback)
  26. } else {
  27. if (completeCallBack) {
  28. completeCallBack();
  29. }
  30. }
  31. },
  32. fail: (err) => {
  33. console.error(err);
  34. if (errorCallback) {
  35. errorCallback(packageName + "加载出错!");
  36. }
  37. }
  38. })
  39. loadTask.onProgressUpdate(res => {
  40. if (progressCallBack) {
  41. progressCallBack(res.progress);
  42. }
  43. })
  44. }
  45. /**
  46. * 微信
  47. * @param packages
  48. * @param progressCallBack
  49. * @param completeCallBack
  50. */
  51. private static __loadWX(packages: string[], progressCallBack: Function, completeCallBack: Function, errorCallback?: Function): void {
  52. console.log("wx subpackage load");
  53. let wx: any = window["wx"];
  54. let packageName: string;
  55. let loadIndex: number = 0;
  56. let total: number = packages.length;
  57. let loadTask: any;
  58. this.loadSubpackage(loadIndex, packages, progressCallBack, completeCallBack, errorCallback)
  59. // for (let index = 0; index < packages.length; index++) {
  60. // packageName = packages[index];
  61. // loadTask = wx.loadSubpackage({
  62. // name: packageName,
  63. // success: (res) => {
  64. // loadIndex++;
  65. // if (loadIndex >= total) {
  66. // if (completeCallBack) {
  67. // completeCallBack();
  68. // }
  69. // }
  70. // },
  71. // fail: (err) => {
  72. // console.error(err);
  73. // if (errorCallback) {
  74. // errorCallback(packageName + "加载出错!");
  75. // }
  76. // }
  77. // })
  78. // loadTask.onProgressUpdate(res => {
  79. // if (progressCallBack) {
  80. // progressCallBack(loadIndex + res.progress);
  81. // }
  82. // })
  83. // }
  84. }
  85. }