Dennis Liu Dennis Liu - 3 months ago 21
C# Question

How to save dictionary data or array list data to playerprefs more effeciently?

Does anyone know how to save dictionary data or array list data to playerprefs more efficiently?

For example I have a class item:

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() {

}
}


Then i will create a list item :

public List<item> items = new List<item> ();


First:
How do you save the items to playerprefs more effecient ?

For example i have data on dictionary:

public Dictionary <string, Dictionary <string, int> > productSellMargaret;


Second:
How to save the productSellMargaret to playerprefs more efficiently?

How do I save both of them to playerprefs so I can continue to call the data when I exit the game and play again with continue data?

Answer

A precise answer would be

"Don't save larger data into PlayerPrefs"

PlayerPrefs is used to save small values like game settings etc. Saving this kind of configurations in PlayerPrefs is a good way to do it.

BUT

When it comes to saving large scale data like lists of objects or nested dictionaries. It is advised to store it into a file.

How should I parse data objects and save it into a file?

  1. There are different ways of doing this. One of them is serializing data objects using BinaryFormatter and saving it in a file using System.IO. For a quick tutorial to achieve this I would refer you to: TutsPlus: How to Save and Load Your Players' Progress in Unity

  2. Again, this is just one way of doing it. You can use other methods as well. Unity's new JSON Serializer is another good option for you.

  3. If you want to work with XML instead of JSON, XML Serializer is a good choice to go with.

Hope this helps.