Commit 93271cb4 authored by liujiangnan's avatar liujiangnan

feat: 预加载功能

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