Commit 93271cb4 authored by liujiangnan's avatar liujiangnan

feat: 预加载功能

parent e417d2cc
...@@ -45,91 +45,69 @@ export default class NewClass extends middleLayerBase { ...@@ -45,91 +45,69 @@ export default class NewClass extends middleLayerBase {
role; role;
// 统一释放异步资源的方法 // 统一释放异步资源的方法
onLoad() { async onLoad() {
this.preloadCount = 0; this.preloadCount = 0;
this.loadedCount = 0; this.loadedCount = 0;
initAir(this); initAir(this);
this.reWriteAir(); this.reWriteAir();
// this.preloadAll();
this.role = 'student'; this.role = 'student';
} }
start() { async start() {
this.node.zIndex = 9999; this.node.zIndex = 9999;
cc.game.addPersistRootNode(this.node); cc.game.addPersistRootNode(this.node);
global.middleLayer = cc.find('middleLayer').getComponent('middleLayer'); global.middleLayer = cc.find('middleLayer').getComponent('middleLayer');
this.loadOnlineCourseWare(23075); // 预加载
// await this.preloadAll();
cc.find(`middleLayer/preload`).active = false;
this.loadOnlineCourseWare(23751);
} }
preloadAll() { async preloadAll() {
if ((<any>window).preloadBundleAndSourcesFlag) { if ((<any>window).preloadBundleAndSourcesFlag) {
// 只加载一次就行了 // 只加载一次就行了
return; return;
} }
(<any>window).preloadBundleAndSourcesFlag = true; (<any>window).preloadBundleAndSourcesFlag = true;
cc.find("middleLayer/ui/ProgressLabel").active = true;
cc.find('middleLayer/ui/loadingProgress').active = true;
let platform = "web_desktop"; let platform = "web_desktop";
if (cc.sys.os == cc.sys.OS_IOS) { if (cc.sys.os == cc.sys.OS_IOS) {
platform = "ios"; platform = "ios";
} else if (cc.sys.os == cc.sys.OS_ANDROID) { } else if (cc.sys.os == cc.sys.OS_ANDROID) {
platform = "android"; platform = "android";
} }
this.callNetworkApiGet(`/api/syllabus/v1/allbundles`, { orgid: 483, platform }, (datastr) => { const datastr:any = await this.asyncCallNetworkApiGet(`/api/syllabus/v1/allbundles`, { orgid: 512, platform });
const data = JSON.parse(datastr); const data = JSON.parse(datastr);
if (data.rows && data.rows.length > 0) { if (data.rows && data.rows.length > 0) {
this.preloadCount += data.rows.length; this.preloadCount += data.rows.length;
} }
this.callNetworkApiGet(`/api/syllabus/v1/allresources`, { orgid: 483 }, (datastr1) => { const datastr1:any = await this.asyncCallNetworkApiGet(`/api/syllabus/v1/allresources`, { orgid: 512 });
const data1 = JSON.parse(datastr1); const data1 = JSON.parse(datastr1);
if (data1.rows && data1.rows.length > 0) { if (data1.rows && data1.rows.length > 0) {
this.preloadCount += data1.rows.length; this.preloadCount += data1.rows.length;
} }
console.log('data.rows = ' + JSON.stringify(data.rows)); console.log('data.rows = ' + JSON.stringify(data.rows));
this.batchPreloadScene(data.rows); await this.preloadSceneAndSource(data, data1);
this.preloadSource(data1.rows);
});
});
} }
// 测试用 preloadSceneAndSource(scenes, sources) {
preloadSourceImman() { const p1 = this.batchPreloadScene(scenes.rows);
cc.find("middleLayer/ui/ProgressLabel").active = true; const p2 = this.preloadSource(sources.rows);
cc.find('middleLayer/ui/loadingProgress').active = true; return Promise.all([p1, p2]);
this.callNetworkApiGet(`/api/courseware/v1/getresources`, { courseid: 20894 }, (datastr1) => {
const data1 = JSON.parse(datastr1);
if (data1.rows && data1.rows.length > 0) {
this.preloadCount += data1.rows.length;
}
this.log('data1.rows = ' + JSON.stringify(data1.rows));
this.preloadSource(data1.rows);
});
} }
batchPreloadScene(scenes = []) { batchPreloadScene(scenes = []) {
const expects = [ return new Promise((resolve) => {
// 'op_03_2', const expects = [
// 'OP10_online', // 'op_03_2',
]; // 'OP10_online',
const _this = this; ];
let i = 0; const _this = this;
const load = function () { let i = 0;
const scene = scenes[i]; const load = function () {
if (expects.includes(scene.sceneName)) { const scene = scenes[i];
_this.loadedCount++; if (expects.includes(scene.sceneName)) {
_this.updateProcessBar();
i++;
if (i < scenes.length) {
setTimeout(() => {
load();
}, 1);
}
return;
}
cc.assetManager.loadBundle(scene.bondleUrl, { version: scene.version }, (err, bundle) => {
console.log(`${scene.bondleUrl}---场景开始loadBundle----`);
if (!bundle) {
_this.loadedCount++; _this.loadedCount++;
_this.updateProcessBar(); _this.updateProcessBar();
i++; i++;
...@@ -137,59 +115,87 @@ export default class NewClass extends middleLayerBase { ...@@ -137,59 +115,87 @@ export default class NewClass extends middleLayerBase {
setTimeout(() => { setTimeout(() => {
load(); load();
}, 1); }, 1);
} else {
resolve();
} }
return; return;
} }
bundle.preloadScene(scene.sceneName, () => { }, () => { cc.assetManager.loadBundle(scene.bondleUrl, { version: scene.version }, (err, bundle) => {
console.log(`${scene.sceneName}场景预加载结束`); console.log(`${scene.bondleUrl}---场景开始loadBundle----`);
_this.loadedCount++; if (!bundle) {
_this.updateProcessBar(); _this.loadedCount++;
i++; _this.updateProcessBar();
if (i < scenes.length) { i++;
setTimeout(() => { if (i < scenes.length) {
load(); setTimeout(() => {
}, 1); load();
}, 1);
} else {
resolve();
}
return;
} }
bundle.preloadScene(scene.sceneName, () => { }, () => {
console.log(`${scene.sceneName}场景预加载结束`);
_this.loadedCount++;
_this.updateProcessBar();
i++;
if (i < scenes.length) {
setTimeout(() => {
load();
}, 1);
} else {
resolve();
}
});
}); });
}); }
}
if (scenes && scenes.length > 0) {
if (scenes && scenes.length > 0) { load();
load(); } else {
} resolve();
}
});
} }
preloadSource(array) { preloadSource(array) {
const rows = array || []; return new Promise((resolve) => {
const batchLoad = () => { const rows = array || [];
let tempArr = rows.splice(0, 10); let loadFlag = 0;
let len = tempArr.length; const batchLoad = () => {
for (let i = 0; i < tempArr.length; i++) { let tempArr = rows.splice(0, 10);
cc.assetManager.loadRemote(tempArr[i], (err, texture) => { let len = tempArr.length;
if (err) { for (let i = 0; i < tempArr.length; i++) {
console.log(err) cc.assetManager.loadRemote(tempArr[i], (err, texture) => {
} if (err) {
this.loadedCount++; console.log(err)
this.updateProcessBar(); }
if (i === len - 1) { this.loadedCount++;
batchLoad(); this.updateProcessBar();
} if (i === len - 1) {
}); batchLoad();
}
loadFlag ++;
if (loadFlag == rows.length) {
resolve();
}
});
}
} }
} batchLoad();
batchLoad(); });
} }
updateProcessBar() { updateProcessBar() {
const label = cc.find("middleLayer/ui/ProgressLabel").getComponent(cc.Label); const label = cc.find("middleLayer/preload/progress/page").getComponent(cc.Label);
const loadingProgress = cc.find('middleLayer/ui/loadingProgress'); label.string = `${Math.floor(this.loadedCount/this.preloadCount*100)}%`;
label.string = `正在加载:${this.loadedCount} / ${this.preloadCount}`;
if (this.loadedCount > this.preloadCount * 0.99) { // 课件加载完成的钩子
cc.find("middleLayer/ui/ProgressLabel").active = false; const process = cc.find(`middleLayer/preload/progress`);
loadingProgress.active = false; const bar = cc.find(`bar`, process);
this.log("资源全部加载完成"); const barWidth = process.width / this.preloadCount * this.loadedCount;
} bar.width = barWidth;
loadingProgress.getComponent(cc.ProgressBar).progress = this.loadedCount / this.preloadCount;
} }
onHomeworkFinish(callBack, data = null) { onHomeworkFinish(callBack, data = null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment