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
}

本文介绍了两种资源管理策略:一次性加载所有资源以避免异步加载,以及根据路径按需获取资源。通过加载目录并创建资源映射,可以高效地管理和检索资源。getRes函数允许根据路径和类型查找资源,提高了代码的灵活性。
3788

被折叠的 条评论
为什么被折叠?



