private loadRemoteDir(url: string, type: typeof cc.Asset) {
return new Promise((resolve, rej) => {
fetch(url)
.then((response) => response.text())
.then((html) => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const b = Array.from(doc.querySelectorAll('a'));
const files = b.map((a) => a.href);
const promise = (path: string, dress: string) => {
const lastIndex = path.lastIndexOf('/');
path = path.substring(lastIndex + 1);
const dre = path.split('.')[1];
path = url + '/' + path;
return new Promise((resolve2) => {
cc.assetManager.loadRemote(path, { ext: '.' + dre }, (err, assets) => {
if (err) {
rej(err);
} else {
resolve2([dress, assets]);
}
});
});
};
const arr: Array<Promise<any>> = [];
let types: Array<string> = [];
switch (type) {
case sp.SkeletonData:
types = ['png', 'atlas', 'json'];
break;
default:
break;
}
files.forEach((fileUrl) => {
const lastIndex = fileUrl.lastIndexOf('/');
const dress = fileUrl.substring(lastIndex + 1).split('.')[1];
if (types.includes(dress)) {
arr.push(promise(fileUrl, dress));
}
});
Promise.all(arr).then((a: Array<any>) => {
const param: any = {};
a.forEach((arr) => {
param[arr[0]] = arr[1];
});
resolve(param);
});
})
.catch((err) => {
rej(err);
});
});
}
cocos2.x加载远程spine资源目录
最新推荐文章于 2024-07-23 14:51:29 发布