-
Creator 版本:2.4.5
-
目标平台: iOS
我在使用localStorage存储数据的时候,遇到个问题,我创建的一个Map里边存的cc.Texture2D[]数组,然后在读取数据的时候就会报错,提示有图片路径不对,我检查了加载资源的时候没有报错,然后在编辑器里也找到了对应的图片,查看了uuid和路径是对的,不知道要怎么处理
先是存数据的方法
saveData() { console.log(this.mapToObj(this.picMap)); cc.sys.localStorage.setItem(STORAGE_KEY, JSON.stringify({ Row3Col2: this.completePicMap.get(ChallengeEnum.Row3Col2), Row4Col2: this.completePicMap.get(ChallengeEnum.Row4Col2), Row4Col3: this.completePicMap.get(ChallengeEnum.Row4Col3), picMap: this.mapToObj(this.picMap), })); }
下面是加载数据的方法
> async loadData() {
> const _data = cc.sys.localStorage.getItem(STORAGE_KEY);
> if (_data) { // 判断localStorage是否有数据
> try {
> const data = JSON.parse(_data);
> this.completePicMap.set(ChallengeEnum.Row3Col2, data.Row3Col2);
> this.completePicMap.set(ChallengeEnum.Row4Col2, data.Row4Col2);
> this.completePicMap.set(ChallengeEnum.Row4Col3, data.Row4Col3);
> this.picMap = await this.objToMap(data.picMap);
> } catch (e) {
> console.error(e);
> }
> } else {
> console.warn('localStorage is empty.');
> }
> console.log(this.picMap);
> }
因为是存Map所以有转obj的方法
mapToObj(map: Map<ChallengeEnum, cc.Texture2D[]>): { [key in ChallengeEnum]: string[] } { const obj = {} as {[key in ChallengeEnum]: string[]}; for (let [key, value] of map) { obj[key] = value.map(texture => texture.nativeUrl); } return obj; } async objToMap(obj: { [key in ChallengeEnum]: string[] }): Promise<Map<ChallengeEnum, cc.Texture2D[]>> { const map = new Map<ChallengeEnum, cc.Texture2D[]>(); for (let key in obj) { const urls = obj[key]; const textures = []; for (let url of urls) { const texture = await new Promise<cc.Texture2D>((resolve, reject) => { cc.resources.load(url, cc.Texture2D, (err, texture: cc.Texture2D) => { if (err) { reject(err); } else { resolve(texture); } }); }); textures.push(texture); } map.set(key as ChallengeEnum, textures); } return map; }
在进入游戏前调加载数据的方法
> async storageLoad() {
> const data = cc.sys.localStorage.getItem(STORAGE_KEY);
> if (data) {
> await DataManager.Instance.loadData();
> }
> }
下面是错误信息
Error: Bundle resources doesn’t contain assets/resources/native/1a/1a166b63-ee5f-4d40-a5bc-f6e68a51ff2a.jpg
at parse (urlTransformer.js:95:39)
at Pipeline.sync (pipeline.js:257:26)
at preprocess (preprocess.js:66:55)
at Pipeline._flow (pipeline.js:308:9)
at Pipeline.async (pipeline.js:302:14)
at AssetManager.loadAny (CCAssetManager.js:492:18)
at Bundle.load (bundle.js:256:25)
at texture (eval at (quick_compile.js:1:1), :201:38)
at new Promise ()
at DataManager.eval (eval at (quick_compile.js:1:1), :200:43)
请大佬帮忙看看,是什么原因,有什么办法解决
