1:一次性加载全部资源,这样就不用每次都异步load了
cc.resources.loadDir("./", (err, assets : cc.Asset[])=>{
let list = cc.resources.getDirWithPath("./")
let uuidDict = {}
for(let item of list) {
uuidDict[item.uuid] = item
}
let resList = {}
for(let asset of assets) {
let path = uuidDict[asset["_uuid"]].path
if(!resList[path]) {
resList[path] = []
}
resList[path].push(asset)
}
this.resList = resList
})
2:根据路径获取对应的资源
getRes(path, type) {
let list = this.resList[path]
if(!list) {
return null
}
for(let item of list) {
if(item instanceof(type)) {
return item
}
}
return null
}