Вот класс с данными
Синтаксис:
Используется csharp
[System.Serializable]
public class ItemData
{
public string Name;
public int Quantity;
public int Defence;
public ItemData(string name, int quantity, int defence)
{
this.Name = name;
this.Quantity = quantity;
this.Defence = defence;
}
}
public class ItemData
{
public string Name;
public int Quantity;
public int Defence;
public ItemData(string name, int quantity, int defence)
{
this.Name = name;
this.Quantity = quantity;
this.Defence = defence;
}
}
Вот класс с предметами, который надо добавлять в инвентарь
Синтаксис:
Используется csharp
public class Items : MonoBehaviour
{
public int[] DefenceItems = new int[] { 5, 10, 15, 20, 25 };
public bool[] HasItems = new bool[] { false, false, false, false, false };
public string[] NameItems = new string[] { "Bandit Pants", "Bulletproof cloak", "Bulletproof cloak Elbow", "Bulletproof cloak wrist", "Military Helmet" };
public int[] Quantity = new int[] { 0, 0, 0, 0, 0 };
public GameObject Character;
public Sprite[] Sprites;
public ItemData ItemDataScript;
[SerializeField] private List<ItemData> items = new List<ItemData>();
private int _currentItem;
private int _currentDefence;
private void Start()
{
LoadItems();
}
public void Equip(int index)
{
if (HasItems[index])
{
_currentItem = index;
_currentDefence = DefenceItems[_currentItem];
Character.GetComponent<SpriteRenderer>().sprite = Sprites[_currentItem];
}
}
public void AddItem(int index)
{
HasItems[index] = true;
if (HasItems[index])
{
Quantity[index]++;
}
SaveItems();//
}
{
public int[] DefenceItems = new int[] { 5, 10, 15, 20, 25 };
public bool[] HasItems = new bool[] { false, false, false, false, false };
public string[] NameItems = new string[] { "Bandit Pants", "Bulletproof cloak", "Bulletproof cloak Elbow", "Bulletproof cloak wrist", "Military Helmet" };
public int[] Quantity = new int[] { 0, 0, 0, 0, 0 };
public GameObject Character;
public Sprite[] Sprites;
public ItemData ItemDataScript;
[SerializeField] private List<ItemData> items = new List<ItemData>();
private int _currentItem;
private int _currentDefence;
private void Start()
{
LoadItems();
}
public void Equip(int index)
{
if (HasItems[index])
{
_currentItem = index;
_currentDefence = DefenceItems[_currentItem];
Character.GetComponent<SpriteRenderer>().sprite = Sprites[_currentItem];
}
}
public void AddItem(int index)
{
HasItems[index] = true;
if (HasItems[index])
{
Quantity[index]++;
}
SaveItems();//
}
Сохранение
Синтаксис:
Используется csharp
public void SaveItems()
{
//List<ItemData> dataList = new List<ItemData>();//
for (int i = 0; i < HasItems.Length; i++)
{
if (HasItems[i])
{
string name = NameItems[i];
int quantity = Quantity[i];
int defence = DefenceItems[i];
ItemDataScript = new ItemData(name, quantity, defence);
items.Add(ItemDataScript);
string json = JsonUtility.ToJson(items);//
File.WriteAllText(Path.Combine(Application.persistentDataPath, "/items.json"), json);
}
}
Загрузка
Синтаксис:
Используется csharp
items.Clear();
string filePath = Path.Combine(Application.persistentDataPath, "/items.json");
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
items = JsonUtility.FromJson<List<ItemData>>(json);
foreach (ItemData data in items)//
{
int index = GetIndex(data.Name);
if (index != -1)
{
HasItems[index] = true;
SetQuantity(data.Name, data.Quantity);
Debug.Log("Ok");
}
else
{
Debug.Log("fail");
}
}
}
}
string filePath = Path.Combine(Application.persistentDataPath, "/items.json");
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
items = JsonUtility.FromJson<List<ItemData>>(json);
foreach (ItemData data in items)//
{
int index = GetIndex(data.Name);
if (index != -1)
{
HasItems[index] = true;
SetQuantity(data.Name, data.Quantity);
Debug.Log("Ok");
}
else
{
Debug.Log("fail");
}
}
}
}
Проверка в методе загрузки при foreach не выводится на консоль.