123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import { _decorator, Component, Node, sys } from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('SubPackageLoader')
- export class SubPackageLoader {
- static load(packages: string[], progressCallBack: Function, completeCallBack: Function, errorCallback?: Function): void {
- console.log("平台:" + sys.browserType);
- // switch (sys.browserType) {
- // case sys.BROWSER_TYPE_WECHAT:
- this.__loadWX(packages, progressCallBack, completeCallBack, errorCallback);
- // break;
- // default:
- // break;
- // }
- }
- private static loadSubpackage(index: number, packages: string[], progressCallBack: Function, completeCallBack: Function, errorCallback?: Function): void {
- let loadTask: any;
- let wx: any = window["wx"];
- let packageName: string = packages[index];
- console.log("加载分包:", packageName)
- index++;
- loadTask = wx.loadSubpackage({
- name: packageName,
- success: (res) => {
- if (index < packages.length) {
- this.loadSubpackage(index, packages, progressCallBack, completeCallBack, errorCallback)
- } else {
- if (completeCallBack) {
- completeCallBack();
- }
- }
- },
- fail: (err) => {
- console.error(err);
- if (errorCallback) {
- errorCallback(packageName + "加载出错!");
- }
- }
- })
- loadTask.onProgressUpdate(res => {
- if (progressCallBack) {
- progressCallBack(res.progress);
- }
- })
- }
- /**
- * 微信
- * @param packages
- * @param progressCallBack
- * @param completeCallBack
- */
- private static __loadWX(packages: string[], progressCallBack: Function, completeCallBack: Function, errorCallback?: Function): void {
- console.log("wx subpackage load");
- let wx: any = window["wx"];
- let packageName: string;
- let loadIndex: number = 0;
- let total: number = packages.length;
- let loadTask: any;
- this.loadSubpackage(loadIndex, packages, progressCallBack, completeCallBack, errorCallback)
- // for (let index = 0; index < packages.length; index++) {
- // packageName = packages[index];
- // loadTask = wx.loadSubpackage({
- // name: packageName,
- // success: (res) => {
- // loadIndex++;
- // if (loadIndex >= total) {
- // if (completeCallBack) {
- // completeCallBack();
- // }
- // }
- // },
- // fail: (err) => {
- // console.error(err);
- // if (errorCallback) {
- // errorCallback(packageName + "加载出错!");
- // }
- // }
- // })
- // loadTask.onProgressUpdate(res => {
- // if (progressCallBack) {
- // progressCallBack(loadIndex + res.progress);
- // }
- // })
- // }
- }
- }
|