Exemple #1
0
        public void Save()
        {
            // Create a new Player_Data.
            Saved_Inventory data = new Saved_Inventory();

            // Save the data.
            for (int i = 0; i < defaultSlotAmount + extraInventorySlots; i++)
            {
                // Assign in order the slot id.
                data.slotID.Add(items [i].ID);
                // IF in the items List we have an ID that isnt -1 so that means we have an actual item.
                // ELSE we have no item.
                if (items [i].ID != -1)
                {
                    // Store the amount of this item.
                    data.slotAmounts.Add(slots [i].GetComponentInChildren <Item_Data> ().amount);
                }
                else
                {
                    // There isn't an item here so add 0.
                    data.slotAmounts.Add(0);
                }
            }
            // Save the extra inventory slots.
            data.extraInventorySlots = extraInventorySlots;
            // Turn the Saved_Inventory data to Json data.
            string inventoryToJson = JsonUtility.ToJson(data);

            // Save the information.
            PlayerPrefs.SetString("Inventory", inventoryToJson);
        }
Exemple #2
0
        private void Load()
        {
            // Grab the information on what is in the inventory.
            string inventoryJson = PlayerPrefs.GetString("Inventory");

            // IF there is nothing in this string.
            if (String.IsNullOrEmpty(inventoryJson))
            {
                // Create the slots on a default level.
                CreateSlots();
                // GTFO of here we done son!
                return;
            }
            // Turn the json data to the data to represent Saved_Inventory.
            Saved_Inventory data = JsonUtility.FromJson <Saved_Inventory> (inventoryJson);

            // Set our extra inventory slots before we create our inventory.
            extraInventorySlots = data.extraInventorySlots;
            // Create the default slots.
            CreateSlots();

            // Loop through however many inventory slots we have.
            for (int i = 0; i < defaultSlotAmount + extraInventorySlots; i++)
            {
                // Fetch the item based on the saved ID for this slot.
                Item fetchedItem = Grid.itemDataBase.FetchItemByID(data.slotID [i]);
                // IF we have an actual item.
                if (fetchedItem != null)
                {
                    // Assign the item to the slot.
                    items [i] = fetchedItem;
                    // Create the item.
                    GameObject itemObj = Instantiate(inventoryItem);
                    // Get the Item_Data component.
                    Item_Data iData = itemObj.GetComponentInChildren <Item_Data> ();
                    // Set the Item_Data item and slot number.
                    iData.item       = fetchedItem;
                    iData.slotNumber = i;
                    // Set the transform.
                    itemObj.transform.SetParent(slots [i].transform);
                    itemObj.transform.localScale    = Vector2.one;
                    itemObj.transform.localPosition = Vector2.zero;
                    // Set the itemObj sprite, sprite color and name of the image.
                    itemObj.GetComponent <Image> ().sprite = fetchedItem.SpriteImage;
                    itemObj.GetComponent <Image> ().color  = new Color(fetchedItem.R, fetchedItem.G, fetchedItem.B, fetchedItem.A);
                    itemObj.name = "Item Slot " + i + " - " + fetchedItem.Title;
                    // IF this fetched item is stackable.
                    if (fetchedItem.Stackable)
                    {
                        // Set the Item_Data amount to what we had saved.
                        iData.amount += data.slotAmounts [i];
                        // Set the text to display how much we have.
                        iData.GetComponentInChildren <Text> ().text = iData.amount.ToString();
                    }
                }
            }
        }