自动图集,不建议放在resrouces目录下,当前把自动图集比如放在assets/atlas/common目录下
在开发模式下,自动图集并没有生成出来,那么在开发调试时,如何使用代码,获取commo自动图集里边的资源?
自动图集,不建议放在resrouces目录下,当前把自动图集比如放在assets/atlas/common目录下
在开发模式下,自动图集并没有生成出来,那么在开发调试时,如何使用代码,获取commo自动图集里边的资源?
逻辑上不需要体现“图集”这个概念,Cocos内部已经自动处理了。
使用和不使用自动图集,代码不需要修改。
比如我在common里边有10个头像icon,我在开发时,更换头像,如何更换?总不能把10个头像都拖到编辑器吧,如何在代码里边切换,sprite.spriteFrame = ?,如何取头像1,头像2…
感谢你的方法,我是想在开发时,动态取图集里边的资源,开发时和发布后如何用一套方法。开发时并没有图集
你不是要使用代码动态加载吗?
通过Asset Bundle,assetManger.loadBundle(‘xxxxx’, (err, bundle) => { bundle.load(…) })
假设给atlas目录设置为bundle,如果common里边有图片被resource里边的对象比如prefab引用,打包发布后,动态图集common就到resource里边了。
这里面涉及一个Bundle优先级的问题,如果atlas的优先级更高,就不是你说的这种情况
改用TexturePacker
还是TexturePacker使用更直观一些
我也用了这种方法,可是bundle.load无法正常地从auto-altas里面加载图片,但在编辑器上可以正常加载。所以路径和图片是没问题的,打包后在浏览器就无法加了,报错信息是“img err Error: Bundle imgBundle doesn’t contain img/bz_icon_1” 所以直接按照散图的方式加载失败了。
后来看到有个网友说用loadDir,我去尝试了一下,ab.loadDir,先去遍历,然后找出对应的图片,这样就能正常加载了。可是这样有点不合理,因为我可能只需要用到一张图片,但是我得先去遍历一遍bundle下的所有图片,这太耗时了。所以我在想可能是我的使用方法错了?下面附上我的代码:
assetManager.loadBundle(‘imgBundle’, (err, ab: AssetManager.Bundle) => {
if (err) {
console.error(bundle err, err);
return;
}
let imgDic: { [key: string]: Asset } = {};
ab.loadDir(“img”, (err, img) => {//编辑器和浏览器均能正常加载到图片
for (let i = 0; i < img.length; i++) {
let item = img[i];
imgDic[item.name] = item;
}
this.sprite.spriteFrame = imgDic[“bz_icon_1”] as SpriteFrame;
});
ab.load('img/bz_icon_1', (err, img) => {//在编辑器能正常加载,打包后在浏览器无法正常加载到图片
if (err) {
console.log(`img err`, err);
return;
}
// const spriteFrame = new SpriteFrame();
// const texture = new Texture2D();
// texture.image = img;
// spriteFrame.texture = texture;
// this.sprite.spriteFrame = spriteFrame;
console.log(`img`, img);
});
});