【发布时间】:2016-11-22 10:00:22
【问题描述】:
有谁知道如何更有效地将字典数据或数组列表数据保存到 playerprefs 中?
例如我有一个类项:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// Make Class Item
public class item {
public string itemName;
public int itemID;
public string itemDesc;
public string itemIcon;
public GameObject itemModel;
public int itemTime;
public int hightprice;
public int stdprice;
public int itemStock;
public int harvest;
public RawTree rawTree;
public ItemType itemType;
public ItemProd itemProd;
public ItemLocation itemLocation;
public ItemMerchant itemMerchant;
public int Lvlunlock;
private string baseName;
public int spawnRate;
public int minqtySpawnBuy;
public int maxqtySpawnBuy;
public int minqtySpawnSell;
public int maxqtySpawnSell;
public int itemExp;
public enum ItemType {
Raw,
Admirable,
Valuable
}
public enum RawTree {
BigTree,
SmallTree,
Field,
None
}
public enum ItemProd {
Corps,
Dairy,
JuiceJamMaker,
Kitchen,
Bakery,
CraftHouse,
ChickenCoop,
FishingSpotMountain,
CowPasture,
LunaMine,
PigPen,
FishingSpotLake,
TropicalWood,
SheepPasture,
FishingSpotSea,
Beebox,
HomeIndustry,
Merchant
}
public enum ItemLocation {
Home,
Orchard,
Forest
}
public enum ItemMerchant {
Margaret,
Lucy,
Bobby,
Roger,
Grace
}
public item (string name, int ID, string desc, int harvestx, int time, int stdpricex, int hightpricex, int stock, int Lvlunlockx, RawTree RawTree, ItemType type, ItemProd prod, string folderx, ItemLocation location, ItemMerchant merchant, int rate, int minspawnBuy, int maxspawnBuy, int minspawnSell, int maxspawnSell, int Exp) {
itemName = name;
itemID = ID;
itemDesc = desc;
harvest = harvestx;
itemTime = time;
stdprice = stdpricex;
hightprice = hightpricex;
itemStock = stock;
Lvlunlock = Lvlunlockx;
rawTree = RawTree;
itemType = type;
itemProd = prod;
itemIcon = folderx;
itemLocation = location;
itemMerchant = merchant;
spawnRate = rate;
minqtySpawnBuy = minspawnBuy;
maxqtySpawnBuy = maxspawnBuy;
minqtySpawnSell = minspawnSell;
maxqtySpawnSell = maxspawnSell;
itemExp = Exp;
}
public item() {
}
}
然后我将创建一个列表项:
public List<item> items = new List<item> ();
首先: 如何更有效地将项目保存到 playerprefs?
例如我有字典上的数据:
public Dictionary <string, Dictionary <string, int> > productSellMargaret;
第二: 如何更有效地将 productSellMargaret 保存到 playerprefs?
如何将它们都保存到 playerprefs 中,以便在退出游戏并使用 continue 数据再次玩游戏时继续调用数据?
【问题讨论】:
-
Playerpref 只能保存某些数据类型。话虽这么说,如果您写入/读取文件(txt 或二进制),它会更快、更高效。
-
将您的对象解析为 JSON 并保存在文件中
-
你好@UmairM,我找到了这个Newtonsoft.Json.Serialization。它可以将其转换为 json。但我不明白如何统一称呼它。我尝试使用 newtons 键入,但它没有使用文件名 newtons。你有什么想法吗?
-
@DennisLiu 检查我的答案