void LoadInventory() { //Debug.Log("LOADING INVENTORY"); string filePath = Application.persistentDataPath + fileName; //string filePath = fileName; if (File.Exists(filePath)) { string dataAsJson = File.ReadAllText(filePath); JsonInventory jsonInventory = JsonUtility.FromJson <JsonInventory>(dataAsJson); Inventory newInventory = new Inventory() { eggs = new List <EggInInventory>(), seeds = new List <SeedInInventory>(), islands = new List <IslandStorage>(), chests = new List <ChestInInventory>() }; newInventory.currency = jsonInventory.currencyAmt; currencyUI.GetComponent <Text>().text = newInventory.currency.ToString(); for (int i = 0; i < jsonInventory.eggs.Length; i++) { EggInInventory newEgg = new EggInInventory() { name = jsonInventory.eggs[i], amt = jsonInventory.eggAmts[i] }; newInventory.eggs.Add(newEgg); } for (int i = 0; i < jsonInventory.seeds.Length; i++) { SeedInInventory newSeed = new SeedInInventory() { name = jsonInventory.seeds[i], amt = jsonInventory.seedAmts[i] }; newInventory.seeds.Add(newSeed); } for (int i = 0; i < jsonInventory.chests.Length; i++) { ChestInInventory newChest = new ChestInInventory() { name = jsonInventory.chests[i], amt = jsonInventory.chestAmts[i] }; newInventory.chests.Add(newChest); } IslandStorage newStorage = new IslandStorage() { fruitInStorage = new List <FruitInInventory>() }; for (int i = 0; i < jsonInventory.fruits0.Length; i++) { FruitInInventory newFruitInInventory = new FruitInInventory(); newFruitInInventory.name = jsonInventory.fruits0[i]; newFruitInInventory.amt = jsonInventory.fruitAmts0[i]; newStorage.fruitInStorage.Add(newFruitInInventory); //Debug.Log("Added fruit"); } newInventory.islands.Add(newStorage); newStorage = new IslandStorage() { fruitInStorage = new List <FruitInInventory>() }; for (int i = 0; i < jsonInventory.fruits1.Length; i++) { FruitInInventory newFruitInInventory = new FruitInInventory(); newFruitInInventory.name = jsonInventory.fruits1[i]; newFruitInInventory.amt = jsonInventory.fruitAmts1[i]; newStorage.fruitInStorage.Add(newFruitInInventory); //Debug.Log("Added fruit"); } newInventory.islands.Add(newStorage); newStorage = new IslandStorage() { fruitInStorage = new List <FruitInInventory>() }; for (int i = 0; i < jsonInventory.fruits0.Length; i++) { FruitInInventory newFruitInInventory = new FruitInInventory(); newFruitInInventory.name = jsonInventory.fruits2[i]; newFruitInInventory.amt = jsonInventory.fruitAmts2[i]; newStorage.fruitInStorage.Add(newFruitInInventory); //Debug.Log("Added fruit"); } newInventory.islands.Add(newStorage); inventory = newInventory; //Debug.Log("LOADED INVENTORY"); } else { SaveInventory(); } }
public void SaveInventory() { //Debug.Log("SAVING INVENTORY"); string filePath = Application.persistentDataPath + fileName; //string filePath = fileName; if (File.Exists(filePath)) { JsonInventory jsonInventory = new JsonInventory() { eggs = new string[inventory.eggs.Count], eggAmts = new int[inventory.eggs.Count], seeds = new string[inventory.seeds.Count], seedAmts = new int[inventory.seeds.Count], chests = new string[inventory.chests.Count], chestAmts = new int[inventory.chests.Count], fruits0 = new string[inventory.islands[0].fruitInStorage.Count], fruitAmts0 = new int[inventory.islands[0].fruitInStorage.Count], fruits1 = new string[inventory.islands[1].fruitInStorage.Count], fruitAmts1 = new int[inventory.islands[1].fruitInStorage.Count], fruits2 = new string[inventory.islands[2].fruitInStorage.Count], fruitAmts2 = new int[inventory.islands[2].fruitInStorage.Count] }; jsonInventory.currencyAmt = inventory.currency; for (int i = 0; i < jsonInventory.eggs.Length; i++) { jsonInventory.eggs[i] = inventory.eggs[i].name; jsonInventory.eggAmts[i] = inventory.eggs[i].amt; } for (int i = 0; i < jsonInventory.seeds.Length; i++) { jsonInventory.seeds[i] = inventory.seeds[i].name; jsonInventory.seedAmts[i] = inventory.seeds[i].amt; } for (int i = 0; i < jsonInventory.chests.Length; i++) { jsonInventory.chests[i] = inventory.chests[i].name; jsonInventory.chestAmts[i] = inventory.chests[i].amt; } for (int i = 0; i < inventory.islands[0].fruitInStorage.Count; i++) { jsonInventory.fruits0[i] = inventory.islands[0].fruitInStorage[i].name; jsonInventory.fruitAmts0[i] = inventory.islands[0].fruitInStorage[i].amt; } for (int i = 0; i < inventory.islands[1].fruitInStorage.Count; i++) { jsonInventory.fruits1[i] = inventory.islands[1].fruitInStorage[i].name; jsonInventory.fruitAmts1[i] = inventory.islands[1].fruitInStorage[i].amt; } for (int i = 0; i < inventory.islands[2].fruitInStorage.Count; i++) { jsonInventory.fruits2[i] = inventory.islands[2].fruitInStorage[i].name; jsonInventory.fruitAmts2[i] = inventory.islands[2].fruitInStorage[i].amt; } string dataAsJson = JsonUtility.ToJson(jsonInventory); File.WriteAllText(filePath, dataAsJson); //Debug.Log("Saved inventory"); } else { File.Create(filePath).Dispose(); SaveInventory(); } }