Commit 603b660d authored by liujiangnan's avatar liujiangnan

feat: 预加载相关

parent b0d768b7
......@@ -2,7 +2,8 @@
import {initAir} from './air';
cc.Class({
extends: cc.Component,
properties: {},
properties: {
},
callNetworkApiPost(uri, data, callBack) {
const xhr = new XMLHttpRequest();
......@@ -38,10 +39,22 @@ cc.Class({
xhr.send();
},
// 需要预加载的数量
preloadCount: null,
// 统计已加载的数量
loadedCount: null,
onLoad() {
this.preloadCount = 0;
this.loadedCount = 0;
initAir(this);
this.preloadBundle();
this.preloadSource();
if(!window.preloadBundleAndSourcesFlag){
// 只加载一次就行了
window.preloadBundleAndSourcesFlag = true;
cc.find("middleLayer/ui/ProgressLabel").active = true;
this.preloadBundle();
this.preloadSource();
}
},
start() {
......@@ -73,16 +86,7 @@ cc.Class({
middleLayer.log("middleLayer.log('像这样在这里打log')");
},
// 需要预加载的数量
preloadCount: 0,
// 统计已加载的数量
loadedCount: 0,
preloadBundle() {
if(window.air.bundleLoaded){
// 限制只加载一次
return;
}
window.air.bundleLoaded = true;
let platform = "web_desktop";
if(cc.sys.os == cc.sys.OS_IOS){
platform = "ios";
......@@ -107,6 +111,7 @@ cc.Class({
bundle.preloadScene(scene.sceneName, ()=>{}, ()=>{
console.log(`${scene.sceneName}场景预加载结束`);
_this.loadedCount ++;
_this.updateProcessBar();
i++;
if(i<scenes.length){
setTimeout(() => {
......@@ -123,11 +128,6 @@ cc.Class({
},
preloadSource() {
if(window.air.sourceLoaded){
// 限制只加载一次
return;
}
window.air.sourceLoaded = true;
this.callNetworkApiGet(`http://staging-teach.ireadabc.com`, `/api/syllabus/v1/allresources`, {orgid: 507}, (datastr) => {
const data = JSON.parse(datastr);
const rows = data.rows;
......@@ -142,6 +142,7 @@ cc.Class({
console.log(err)
}
this.loadedCount ++;
this.updateProcessBar();
if (i === len-1) {
batchLoad();
}
......@@ -153,6 +154,14 @@ cc.Class({
});
},
updateProcessBar() {
const label = cc.find("middleLayer/ui/ProgressLabel").getComponent(cc.Label);
label.string = `正在加载:${this.loadedCount} / ${this.preloadCount}`;
if(this.loadedCount > this.preloadCount * 0.99){
cc.find("middleLayer/ui/ProgressLabel").active = false;
}
},
asyncDelayLog(str) {
console.log(str);
return new Promise((resolve, reject) => {
......
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