2025-04-24 17:03:28 +08:00

157 lines
6.5 KiB
TypeScript

import SKLoadData, { SKLoadType } from "./SKLoadData";
import FGUtil from "../fgui/FGUtil";
export default class SKLoader
{
static shared=new SKLoader();
progressBlock:(data:SKLoadData,progress:number)=>void;
completeBlock:(data:SKLoadData,resource:any[],urls:string[])=>void;
doneBlock:()=>void;
list:SKLoadData[];
current:number;
isLoading:boolean;
load(list:SKLoadData[],progress:(data:SKLoadData,progress:number)=>void,complete:(data:SKLoadData,resource:any[],urls:any[])=>void,done:()=>void){
this.list=list;
this.progressBlock=progress;
this.completeBlock=complete;
this.doneBlock=done;
this.current=0;
this.isLoading=true;
let self=this;
for(let data of list){
switch(data.type){
case SKLoadType.Gui:
FGUtil.load("gui/main_ui",(progress:number)=>{
if(isNaN(progress)){
progress=0;
}
self.progressBlock(data,progress);
},(error:Error)=>{
if(error){
cc.error(error.message);
self.current++;
self.checkDone(data.url);
return;
}
self.completeBlock(data,null,null);
self.current++;
self.checkDone(data.url);
});
break;
case SKLoadType.Json:
cc.loader.loadRes(data.url,cc.JsonAsset,(completedCount:number,totalCount:number,item:any)=>{
let progress=0;
if(totalCount>0){
progress=completedCount/totalCount;
}
self.progressBlock(data,progress);
},(error: Error, resource: any)=>{
if(error){
cc.error(error.message);
self.current++;
self.checkDone(data.url);
return;
}
self.completeBlock(data,[resource],null);
self.current++;
self.checkDone(data.url);
});
break;
case SKLoadType.Text:
cc.loader.loadRes(data.url,cc.TextAsset,(completedCount:number,totalCount:number,item:any)=>{
let progress=0;
if(totalCount>0){
progress=completedCount/totalCount;
}
self.progressBlock(data,progress);
},(error: Error, resource: any)=>{
if(error){
cc.error(error.message);
self.current++;
self.checkDone(data.url);
return;
}
self.completeBlock(data,[resource],null);
self.current++;
self.checkDone(data.url);
});
break;
case SKLoadType.Prefab:
cc.loader.loadRes(data.url,cc.Prefab,(completedCount:number,totalCount:number,item:any)=>{
let progress=0;
if(totalCount>0){
progress=completedCount/totalCount;
}
self.progressBlock(data,progress);
},(error: Error, resource: any)=>{
if(error){
cc.error(error.message);
self.current++;
self.checkDone(data.url);
return;
}
self.completeBlock(data,[resource],null);
self.current++;
self.checkDone(data.url);
});
break;
case SKLoadType.Atlas:
cc.loader.loadRes(data.url,cc.SpriteAtlas,(completedCount:number,totalCount:number,item:any)=>{
let progress=0;
if(totalCount>0){
progress=completedCount/totalCount;
}
self.progressBlock(data,progress);
},(error: Error, resource: any)=>{
if(error){
cc.error(error.message);
self.current++;
self.checkDone(data.url);
return;
}
self.completeBlock(data,[resource],null);
self.current++;
self.checkDone(data.url);
});
break;
case SKLoadType.Dir:
if(data.type === SKLoadType.Dir){
cc.loader.loadResDir(data.url,(completedCount:number,totalCount:number,item:any)=>{
let progress:number=completedCount/totalCount;
self.progressBlock(data,progress);
},(error:Error,resources:any[],urls:string[])=>{
if(error){
cc.error(error.message);
self.current++;
self.checkDone(data.url);
return;
}
self.completeBlock(data,resources,urls);
self.current++;
self.checkDone(data.url);
});
}
break;
default:
cc.log(`未知的加載類型:${data.type}`);
self.current++;
self.checkDone(data.url);
break;
}
}
}
// 檢查是否全部加載完成
private checkDone(url:string){
if(!this.isLoading){
return;
}
cc.log(`檢查加載完成:[${url}]${this.current}/${this.list.length}`);
if(this.current>=this.list.length){
this.isLoading=false;
this.doneBlock();
return;
}
}
}