cocos2.x加载远程spine资源目录

 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);
                });
        });
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值