I'd design a Unity inventory system using a modular, event-driven architecture. Here's my approach:
Core Data Structure
Start with a scriptable object for items:
csharp
[CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
public string itemName;
public Sprite icon;
public int maxStackSize = 1;
public ItemType type;
public string description;
}
Inventory Manager
Create a central inventory class that handles the core logic:
csharp
public class Inventory : MonoBehaviour
{
[SerializeField] private List<InventorySlot> slots = new List<InventorySlot>();
public static event System.Action<Item, int> OnItemAdded;
public static event System.Action<Item, int> OnItemRemoved;
public bool AddItem(Item item, int quantity = 1)
{
// Try to stack first, then find empty slots
// Fire events on success
}
public bool RemoveItem(Item item, int quantity = 1)
{
// Remove items and fire events
}
}
[System.Serializable]
public class InventorySlot
{
public Item item;
public int quantity;
public bool IsEmpty => item == null;
}
Event-Driven Communication: Use UnityEvents or C# events so different systems can react to inventory changes without tight coupling.
Scriptable Objects: Store item data in scriptable objects for easy designer workflow and memory efficiency.
Separation of Concerns: Keep data logic separate from UI logic. The inventory should work without any UI attached.
Flexible Slot System: Design slots to handle different item types, stack sizes, and special properties.
Advanced Features
For more complex games, consider adding:
Item Categories: Equipment, consumables, key items with different behaviors
Drag and Drop: Implement IDragHandler and IDropHandler interfaces
Filtering and Sorting: Allow players to organize their inventory
Equipment System: Separate equipped items from general inventory
Persistence: Save/load using JsonUtility or custom serialization
Performance Considerations
Use object pooling for UI slots if you have many items
Cache frequently accessed data
Consider using structs for simple item data to reduce garbage collection
Implement dirty flags to avoid unnecessary UI updates
This architecture scales well from simple RPG inventories to complex crafting systems, and the event system makes it easy to integrate with other game systems like crafting, shops, or quest systems.