
This post contains all the code and JSON that’s been written in the YouTube video.
You can read it at your own pace, although watching the video prior to looking at this code example is recommended.
Inventory.cs
using System.Collections.Generic;
using UnityEngine;
using Boomlagoon.JSON;
public class Inventory : MonoBehaviour {
public static Inventory Instance { get; private set; }
private JSONObject itemsData;
public static int SelectedItemIndex { get; private set; }
public class ShopItem
{
public bool Bought, Selected;
public int Price;
public string Name;
public ShopItem (bool bought, bool selected, int price, string name)
{
Bought = bought; Selected = selected; Price = price; Name = name;
}
}
public static List<ShopItem> Items;
public static int Coins { get; private set; }
private void Awake()
{
Instance = this;
if (!PlayerPrefs.HasKey("Items"))
{
PlayerPrefs.SetString("Items", "{\"Items\":[{\"name\":\"Knife\",\"bought\":true,\"selected\":true,\"price\":100},{\"name\":\"Rifle\",\"bought\":false,\"selected\":false,\"price\":500},{\"name\":\"Bow\",\"bought\":false,\"selected\":false,\"price\":150},{\"name\":\"Machete\",\"bought\":false,\"selected\":false,\"price\":200},{\"name\":\"Bazooka\",\"bought\":false,\"selected\":false,\"price\":1000}]}");
PlayerPrefs.SetInt("Coins", 5000);
}
Coins = PlayerPrefs.GetInt("Coins");
itemsData = JSONObject.Parse(PlayerPrefs.GetString("Items"));
Items = new List<ShopItem>();
for (int i = 0; i < itemsData.GetArray("Items").Length; i++)
{
Items.Add(new ShopItem(itemsData.GetArray("Items")[i].Obj.GetBoolean("bought"),
itemsData.GetArray("Items")[i].Obj.GetBoolean("selected"),
(int)itemsData.GetArray("Items")[i].Obj.GetNumber("price"),
itemsData.GetArray("Items")[i].Obj.GetString("name")));
if (Items[i].Selected)
SelectedItemIndex = i;
}
}
public void SelectItem(int index)
{
for (int i = 0; i < Items.Count; i++)
{
if (Items[i].Selected)
{
Items[i].Selected = false;
itemsData.GetArray("Items")[i].Obj.GetValue("selected").Boolean = false;
}
}
Items[index].Selected = true;
itemsData.GetArray("Items")[index].Obj.GetValue("selected").Boolean = true;
SelectedItemIndex = index;
PlayerPrefs.SetString("Items", itemsData.ToString());
PlayerPrefs.Save();
}
public void BuyItem(int index)
{
if (SubtractCoins(Items[index].Price))
{
Items[index].Bought = true;
itemsData.GetArray("Items")[index].Obj.GetValue("bought").Boolean = true;
SelectItem(index);
}
}
private bool SubtractCoins(int value)
{
if (Coins - value < 0)
return false;
Coins -= value;
PlayerPrefs.SetInt("Coins", Coins);
return true;
}
}
Items.json
{
"Items": [
{
"name": "Knife",
"bought": true,
"selected": true,
"price": 100
},
{
"name": "Rifle",
"bought": false,
"selected": false,
"price": 500
},
{
"name": "Bow",
"bought": false,
"selected": false,
"price": 150
},
{
"name": "Machete",
"bought": false,
"selected": false,
"price": 200
},
{
"name": "Bazooka",
"bought": false,
"selected": false,
"price": 1000
}
]
}

blackhatlinks.com
gcnrjtonk tnxls ezspwwi ueqb xqwsqpztnkpdfbp
Your article helped me a lot, is there any more related content? Thanks!
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your article helped me a lot, is there any more related content? Thanks!
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://accounts.binance.com/uk-UA/register-person?ref=W0BCQMF1
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Your article helped me a lot, is there any more related content? Thanks!