/// <summary> /// 保存JSON数据到本地的方法 /// </summary> public void Save(Good_materials gd) { //打包后Resources文件夹不能存储文件,如需打包后使用自行更换目录 string filePath = Application.persistentDataPath + @"/GoodData.json"; //Debug.Log(Application.persistentDataPath + @"/GoodData.json"); goodList.dictionary = Load(); if (!File.Exists(filePath)) //不存在就创建键值对 { goodList.dictionary.Add(gd.g_name, gd); } else if (goodList.dictionary != null) //若存在就更新值 { goodList.dictionary[gd.g_name] = gd; } //找到当前路径 FileInfo file = new FileInfo(filePath); //判断有没有文件,有则打开文件,,没有创建后打开文件 StreamWriter sw = file.CreateText(); //ToJson接口将你的列表类传进去,,并自动转换为string类型 string json = JsonMapper.ToJson(goodList.dictionary); //将转换好的字符串存进文件, sw.WriteLine(json); //注意释放资源 sw.Close(); sw.Dispose(); // AssetDatabase.Refresh(); }
public void 物品持久化(string 物品, int 数量) { //持久化对象 Good_materials gd; GoodDataSave save = new GoodDataSave(); //先读取json中物品的数据 Dictionary <string, Good_materials> D_gd = save.Load(); //无则新建 gd = new Good_materials(物品, 数量); //有则修改 if (D_gd != null && D_gd.ContainsKey(物品)) { Good_materials temp = D_gd[物品]; gd = new Good_materials(temp.g_name, temp.get_num + 数量); } save.Save(gd); }