public void Load() { print("Load started"); var itemsToDestroy = FindObjectsOfType <Item>(); var sceneLootBoxes = FindObjectsOfType <LootBox>(); foreach (var item in itemsToDestroy) { Destroy(item.gameObject); } foreach (var lootbox in sceneLootBoxes) { lootbox.lootBoxItems.Clear(); } //Inventory DTInventory inventory = FindObjectOfType <DTInventory>(); InventoryData inventoryData = JsonUtility.FromJson <InventoryData>(File.ReadAllText(Application.persistentDataPath + "/" + SceneManager.GetActiveScene().name + "_inventoryData")); var inventoryItems = inventoryData.itemNames; var stackSize = inventoryData.stackSize; var itemPos = inventoryData.itemGridPos; bool isAutoEquipEnabled = inventory.autoEquipItems; inventory.autoEquipItems = false; if (inventoryItems != null) { for (int i = 0; i < inventoryItems.Length; i++) { var findItem = assetsDatabase.FindItem(inventoryItems[i]); if (findItem != null) { var item = Instantiate(findItem); item.stackSize = stackSize[i]; inventory.AddItem(item, (int)itemPos[i].x, (int)itemPos[i].y); } else { Debug.LogAssertion("Missing item. Check if it exists in the ItemsDatabase inspector"); } } } inventory.autoEquipItems = isAutoEquipEnabled; print("Looking for data for scene " + SceneManager.GetActiveScene().name); LevelData itemsLevelData = JsonUtility.FromJson <LevelData>(File.ReadAllText(Application.persistentDataPath + "/" + SceneManager.GetActiveScene().name + "_itemsLevelData")); for (int i = 0; i < itemsLevelData.itemName.Length; i++) { if (itemsLevelData.itemName[i] != null) { try { var item = Instantiate(assetsDatabase.FindItem(itemsLevelData.itemName[i])); item.transform.position = itemsLevelData.itemPos[i]; item.transform.rotation = itemsLevelData.itemRot[i]; item.stackSize = itemsLevelData.itemStackSize[i]; } catch { Debug.LogAssertion("Item you try to restore from save: " + itemsLevelData.itemName[i] + " is null or not exist in database"); } } } LootBoxData lootBoxData = JsonUtility.FromJson <LootBoxData>(File.ReadAllText(Application.persistentDataPath + "/" + SceneManager.GetActiveScene().name + "_lootboxData")); for (int i = 0; i < sceneLootBoxes.Length; i++) { var lootbox = sceneLootBoxes[i]; char[] separator = new char[] { '|' }; string[] itemsTitles = lootBoxData.itemNames[i].Split(separator, System.StringSplitOptions.RemoveEmptyEntries); //foreach (string t in itemsTitles) // print(t); string[] itemStackSizes = lootBoxData.stackSize[i].Split(separator, System.StringSplitOptions.RemoveEmptyEntries); //foreach (string jk in itemStackSizes) // print(jk); List <int> itemStackSizesInt = new List <int>(); foreach (string itemStackSizeString in itemStackSizes) { int resultInt = -1; int.TryParse(itemStackSizeString, out resultInt); itemStackSizesInt.Add(resultInt); } for (int j = 0; j < itemsTitles.Length; j++) { if (assetsDatabase.FindItem(itemsTitles[j]) != null) { var item = Instantiate(assetsDatabase.FindItem(itemsTitles[j])); item.gameObject.SetActive(false); if (itemStackSizesInt[j] > -1) { item.stackSize = itemStackSizesInt[j]; } lootbox.lootBoxItems.Add(item); } } } }
public void Save() { //Save inventory items var sceneItems = FindObjectsOfType <InventoryItem>(); List <string> items = new List <string>(); List <int> stacksize = new List <int>(); List <Vector2> itemGridPos = new List <Vector2>(); List <Vector2> itemRectPos = new List <Vector2>(); foreach (var i_item in sceneItems) { items.Add(i_item.item.title); stacksize.Add(i_item.item.stackSize); itemGridPos.Add(new Vector2(i_item.x, i_item.y)); itemRectPos.Add(i_item.GetComponent <RectTransform>().anchoredPosition); } var _i = items.ToArray(); var _s = stacksize.ToArray(); var _p = itemGridPos.ToArray(); InventoryData inventoryData = new InventoryData(_i, _s, _p); string _inventoryData = JsonUtility.ToJson(inventoryData); File.WriteAllText(Application.persistentDataPath + "/" + SceneManager.GetActiveScene().name + "_inventoryData", _inventoryData); //Save scene items var allSceneItems = FindObjectsOfType <Item>(); List <Item> enabledItems = new List <Item>(); foreach (var item in allSceneItems) { if (item.isActiveAndEnabled) { enabledItems.Add(item); } } LevelData itemsLevelData = new LevelData(); itemsLevelData.itemName = new string[enabledItems.ToArray().Length]; itemsLevelData.itemPos = new Vector3[enabledItems.ToArray().Length]; itemsLevelData.itemRot = new Quaternion[enabledItems.ToArray().Length]; itemsLevelData.itemStackSize = new int[enabledItems.ToArray().Length]; for (int i = 0; i < enabledItems.ToArray().Length; i++) { itemsLevelData.itemName[i] = enabledItems.ToArray()[i].title; itemsLevelData.itemPos[i] = enabledItems.ToArray()[i].transform.position; itemsLevelData.itemRot[i] = enabledItems.ToArray()[i].transform.rotation; itemsLevelData.itemStackSize[i] = enabledItems.ToArray()[i].stackSize; } string _itemsLevelData = JsonUtility.ToJson(itemsLevelData); //print(_itemsLevelData); File.WriteAllText(Application.persistentDataPath + "/" + SceneManager.GetActiveScene().name + "_itemsLevelData", _itemsLevelData); //Save lootbox items var allSceneLootboxes = FindObjectsOfType <LootBox>(); List <string> loot_ItemNames = new List <string>(); List <string> loot_ItemsCount = new List <string>(); foreach (LootBox lootBox in allSceneLootboxes) { string itemsString = string.Empty; string itemsStacksize = string.Empty; foreach (Item item in lootBox.lootBoxItems) { itemsString = itemsString + item.title + "|"; itemsStacksize = itemsStacksize + item.stackSize.ToString() + "|"; } loot_ItemNames.Add(itemsString); loot_ItemsCount.Add(itemsStacksize); } LootBoxData lootBoxData = new LootBoxData(); lootBoxData.itemNames = loot_ItemNames.ToArray(); lootBoxData.stackSize = loot_ItemsCount.ToArray(); string _lootBoxData = JsonUtility.ToJson(lootBoxData); File.WriteAllText(Application.persistentDataPath + "/" + SceneManager.GetActiveScene().name + "_lootboxData", _lootBoxData); }