0% found this document useful (0 votes)
10 views3 pages

Inventory Object

This document defines C# classes for an inventory system in Unity. The InventoryObject class manages an Inventory and allows adding, removing, moving and saving/loading items. Inventory is a list of InventorySlots, each containing an item ID, reference and amount. InventoryObject handles checking for empty slots and updating amounts when adding items, as well as saving/loading the inventory data to file using binary serialization.

Uploaded by

claudio castro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Inventory Object

This document defines C# classes for an inventory system in Unity. The InventoryObject class manages an Inventory and allows adding, removing, moving and saving/loading items. Inventory is a list of InventorySlots, each containing an item ID, reference and amount. InventoryObject handles checking for empty slots and updating amounts when adding items, as well as saving/loading the inventory data to file using binary serialization.

Uploaded by

claudio castro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEditor;
using System.Runtime.Serialization;

[CreateAssetMenu(fileName = "New Inventory", menuName = "Inventory


System/Inventory")]
public class InventoryObject : ScriptableObject
{
public string savePath;
public ItemDatabaseObject database;
public Inventory Container;

public void AddItem(Item _item, int _amount)


{
if (_item.buffs.Length > 0)
{
SetEmptySlot(_item, _amount);
return;
}

for (int i = 0; i < Container.Items.Length; i++)


{
if (Container.Items[i].ID == _item.Id)
{
Container.Items[i].AddAmount(_amount);
return;
}
}
SetEmptySlot(_item, _amount);

}
public InventorySlot SetEmptySlot(Item _item, int _amount)
{
for (int i = 0; i < Container.Items.Length; i++)
{
if(Container.Items[i].ID <= -1)
{
Container.Items[i].UpdateSlot(_item.Id, _item, _amount);
return Container.Items[i];
}
}
//set up functionality for full inventory
return null;
}

public void MoveItem(InventorySlot item1, InventorySlot item2)


{
InventorySlot temp = new InventorySlot(item2.ID, item2.item, item2.amount);
item2.UpdateSlot(item1.ID, item1.item, item1.amount);
item1.UpdateSlot(temp.ID, temp.item, temp.amount);
}

public void RemoveItem(Item _item)


{
for (int i = 0; i < Container.Items.Length; i++)
{
if(Container.Items[i].item == _item)
{
Container.Items[i].UpdateSlot(-1, null, 0);
}
}
}

[ContextMenu("Save")]
public void Save()
{
//string saveData = JsonUtility.ToJson(this, true);
//BinaryFormatter bf = new BinaryFormatter();
//FileStream file =
File.Create(string.Concat(Application.persistentDataPath, savePath));
//bf.Serialize(file, saveData);
//file.Close();

IFormatter formatter = new BinaryFormatter();


Stream stream = new
FileStream(string.Concat(Application.persistentDataPath, savePath),
FileMode.Create, FileAccess.Write);
formatter.Serialize(stream, Container);
stream.Close();
}
[ContextMenu("Load")]
public void Load()
{
if(File.Exists(string.Concat(Application.persistentDataPath, savePath))){
//BinaryFormatter bf = new BinaryFormatter();
//FileStream file =
File.Open(string.Concat(Application.persistentDataPath, savePath), FileMode.Open);
//JsonUtility.FromJsonOverwrite(bf.Deserialize(file).ToString(), this);
//file.Close();

IFormatter formatter = new BinaryFormatter();


Stream stream = new
FileStream(string.Concat(Application.persistentDataPath, savePath), FileMode.Open,
FileAccess.Read);
Inventory newContainer = (Inventory)formatter.Deserialize(stream);
for (int i = 0; i < Container.Items.Length; i++)
{
Container.Items[i].UpdateSlot(newContainer.Items[i].ID,
newContainer.Items[i].item, newContainer.Items[i].amount);
}
stream.Close();
}
}
[ContextMenu("Clear")]
public void Clear()
{
Container = new Inventory();
}
}
[System.Serializable]
public class Inventory
{
public InventorySlot[] Items = new InventorySlot[28];
}
[System.Serializable]
public class InventorySlot
{
public int ID = -1;
public Item item;
public int amount;
public InventorySlot()
{
ID = -1;
item = null;
amount = 0;
}
public InventorySlot(int _id, Item _item, int _amount)
{
ID = _id;
item = _item;
amount = _amount;
}
public void UpdateSlot(int _id, Item _item, int _amount)
{
ID = _id;
item = _item;
amount = _amount;
}
public void AddAmount(int value)
{
amount += value;
}
}

You might also like