cocos-animator/animator-editor/assets/script/common/util/Res.ts

69 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-01-19 14:30:12 +00:00
/**
*
*/
export default class Res {
/**
*
*/
private static _cacheMap: Map<string, cc.Asset> = new Map();
/**
*
* @param url
*/
public static getLoaded(url: string): any {
let asset = this._cacheMap.get(url);
if (asset === undefined) {
cc.error(`[Res.getLoaded] error: 资源未加载`);
return null;
}
return asset;
}
/**
* resources文件夹下单个资源
* @param url
* @param type
*/
public static async load(url: string, type: typeof cc.Asset): Promise<any> {
let asset = this._cacheMap.get(url);
if (asset) {
return asset;
}
return await new Promise((resolve, reject) => {
cc.loader.loadRes(url, type, (error: Error, resource: cc.Asset) => {
if (error) {
cc.error(`[Res.load] error: ${error}`);
resolve(null);
} else {
this._cacheMap.set(url, resource);
resolve(resource);
}
});
});
}
/**
* resources文件夹下某个文件夹内全部资源
* @param url
* @param type
*/
public static async loadDir(url: string, type: typeof cc.Asset): Promise<any[]> {
return await new Promise((resolve, reject) => {
cc.loader.loadResDir(url, type, (error: Error, resource: any[], urls: string[]) => {
if (error) {
cc.error(`[Res.loadDir] error: ${error}`);
resolve([]);
} else {
urls.forEach((v: string, i: number) => {
this._cacheMap.set(v, resource[i]);
});
resolve(resource);
}
});
});
}
}