An open API service indexing awesome lists of open source software.

https://github.com/pietras333/inventory-system

A robust, SOLID-principle-based inventory system for Unity games featuring drag-and-drop functionality, item stacking, splitting, and merging capabilities.
https://github.com/pietras333/inventory-system

csharp gamedev inventory-system unity3d

Last synced: 12 months ago
JSON representation

A robust, SOLID-principle-based inventory system for Unity games featuring drag-and-drop functionality, item stacking, splitting, and merging capabilities.

Awesome Lists containing this project

README

          

# ๐ŸŽฎ Unity Inventory System

A robust, SOLID-principle-based inventory system for Unity games featuring drag-and-drop functionality, item stacking, splitting, and merging capabilities.

## โœจ Features

- ๐Ÿ–ฑ๏ธ **Drag & Drop Interface** - Intuitive mouse-based item management
- ๐Ÿ“ฆ **Item Stacking** - Automatic stacking of identical items up to max stack size
- โœ‚๏ธ **Item Splitting** - Right-click to split item stacks in half
- ๐Ÿ”„ **Item Merging** - Combine compatible items automatically
- ๐Ÿ” **Item Swapping** - Swap items between slots seamlessly
- ๐Ÿ’ก **Tooltip System** - Hover tooltips with item information
- ๐Ÿ—๏ธ **SOLID Architecture** - Clean, maintainable, and extensible code structure
- ๐Ÿ”ง **ScriptableObject Configuration** - Easy customization without code changes
- ๐ŸŒ **Network Ready** - Built with Unity Netcode integration in mind

## ๐Ÿ› ๏ธ Dependencies

https://github.com/user-attachments/assets/dfa65463-711f-4976-9297-323d5d0a7070

### Unity Version
- **Unity 2022.3 LTS** or higher

### Required Packages
```json
{
"com.unity.textmeshpro": "3.0.6",
"com.unity.netcode.gameobjects": "1.7.1"
}
```

### Installation via Package Manager
1. Open Unity Package Manager (`Window > Package Manager`)
2. Install **TextMeshPro** from Unity Registry
3. Install **Netcode for GameObjects** from Unity Registry

## ๐Ÿ“ Project Structure

```
Assets/
โ”œโ”€โ”€ ๐Ÿ“‚ Scripts/
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ Core/
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ”ง ItemDefinition.cs
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ”ง ItemContainerConfig.cs
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ”ง ItemContainerSlotUIConfig.cs
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ Inventory/
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“ฆ ItemContainer.cs
โ”‚ โ”‚ โ”œโ”€โ”€ ๐ŸŽฏ ItemContainerItemUI.cs
โ”‚ โ”‚ โ””โ”€โ”€ ๐ŸŽช ItemContainerSlotUI.cs
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ DragSystem/
โ”‚ โ”‚ โ”œโ”€โ”€ ๐ŸŽฎ ItemDragManager.cs
โ”‚ โ”‚ โ”œโ”€โ”€ ๐ŸŽฏ InventoryInputHandler.cs
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ”ง ItemDragService.cs
โ”‚ โ”‚ โ”œโ”€โ”€ โš™๏ธ ItemOperationService.cs
โ”‚ โ”‚ โ””โ”€โ”€ ๐ŸŽจ DragVisualizer.cs
โ”‚ โ””โ”€โ”€ ๐Ÿ“‚ UI/
โ”‚ โ””โ”€โ”€ ๐Ÿ’ฌ TooltipUI.cs
โ”œโ”€โ”€ ๐Ÿ“‚ Prefabs/
โ”‚ โ”œโ”€โ”€ ๐Ÿ“ฆ InventoryContainer.prefab
โ”‚ โ”œโ”€โ”€ ๐ŸŽช InventorySlot.prefab
โ”‚ โ””โ”€โ”€ ๐ŸŽฎ ItemDragManager.prefab
โ””โ”€โ”€ ๐Ÿ“‚ ScriptableObjects/
โ”œโ”€โ”€ ๐Ÿ“‚ Items/
โ”‚ โ”œโ”€โ”€ โš”๏ธ Sword.asset
โ”‚ โ”œโ”€โ”€ ๐Ÿ›ก๏ธ Shield.asset
โ”‚ โ””โ”€โ”€ ๐Ÿงช Potion.asset
โ””โ”€โ”€ ๐Ÿ“‚ Configs/
โ”œโ”€โ”€ ๐Ÿ”ง DefaultContainerConfig.asset
โ””โ”€โ”€ ๐ŸŽจ DefaultSlotConfig.asset
```

## ๐Ÿš€ Quick Start

### 1. Setup Item Definitions
```csharp
// Create items via: Assets > Create > Inventory System > Item
// Configure: ID, Name, Description, Icon, Max Stack Size, Prefab
```

### 2. Create Inventory Container
```csharp
// 1. Create empty GameObject
// 2. Add ItemContainer component
// 3. Assign ItemContainerConfig
// 4. Add ItemContainerSlotUI components as children
// 5. Assign slots to the Slots list in ItemContainer
```

### 3. Setup Drag Manager
```csharp
// 1. Create ItemDragManager prefab in scene
// 2. Assign DragVisualizer component
// 3. Configure drag visual elements (Image, Text)
```

### 4. Configure UI Elements
```csharp
// Each slot needs:
// - ItemContainerSlotUI component
// - UI Image for slot background
// - UI Image for held item display
// - TextMeshProUGUI for amount display
```

## ๐ŸŽฏ Usage Examples

### Adding Items to Inventory
```csharp
public class InventoryManager : MonoBehaviour
{
[SerializeField] private ItemContainer playerInventory;
[SerializeField] private ItemDefinition swordItem;

void Start()
{
// Add 5 swords to inventory
playerInventory.TryAddItem(swordItem, 5);
}
}
```

### Custom Item Operations
```csharp
public class CustomItemOperations : MonoBehaviour, IItemOperationService
{
public bool TryMoveItem(ItemContainerItemUI item, ItemContainerSlotUI targetSlot)
{
// Add custom logic (e.g., equipment restrictions)
if (item.ItemDefinition.ItemType == ItemType.Weapon && targetSlot.SlotType != SlotType.WeaponSlot)
return false;

return defaultOperationService.TryMoveItem(item, targetSlot);
}

// Implement other interface methods...
}
```

## ๐ŸŽจ Customization

### Item Configuration
Create new items through the ScriptableObject menu:
- **Right-click in Project** โ†’ **Create** โ†’ **Inventory System** โ†’ **Item**

### Visual Customization
- **Slot Icons**: Configure active/passive slot sprites in `ItemContainerSlotUIConfig`
- **Drag Visuals**: Customize drag appearance in `DragVisualizer` component
- **Tooltips**: Modify `TooltipUI` for custom tooltip styles

### Extending Functionality
```csharp
// Add new item operations
public class EnchantmentService : IItemOperationService
{
public bool TryEnchantItem(ItemContainerItemUI item, EnchantmentType enchantment)
{
// Custom enchantment logic
return true;
}
}
```

## ๐Ÿ—๏ธ Architecture Overview

### SOLID Principles Implementation

- **๐Ÿ”ง Single Responsibility**: Each class has one clear purpose
- **๐Ÿ“– Open/Closed**: Easy to extend without modifying existing code
- **๐Ÿ”„ Liskov Substitution**: Interfaces can be swapped seamlessly
- **๐ŸŽฏ Interface Segregation**: Focused, specific interfaces
- **๐Ÿ”— Dependency Inversion**: Depends on abstractions, not concrete classes

### Key Components

| Component | Responsibility |
|-----------|---------------|
| `ItemDragService` | ๐ŸŽฏ Manages drag state |
| `ItemOperationService` | โš™๏ธ Handles item operations |
| `DragVisualizer` | ๐ŸŽจ Visual drag representation |
| `InventoryInputHandler` | ๐ŸŽฎ Input processing |
| `ItemDragManager` | ๐ŸŽช Facade coordinator |

## ๐Ÿงช Testing

### Unit Testing Setup
```csharp
[Test]
public void TestItemMerging()
{
// Arrange
var mockDragService = new Mock();
var operationService = new ItemOperationService();

// Act & Assert
Assert.IsTrue(operationService.TryMergeItems(item1, slot));
}
```

## ๐Ÿค Contributing

1. ๐Ÿด Fork the repository
2. ๐ŸŒŸ Create a feature branch (`git checkout -b feature/amazing-feature`)
3. ๐Ÿ’พ Commit changes (`git commit -m 'Add amazing feature'`)
4. ๐Ÿ“ค Push to branch (`git push origin feature/amazing-feature`)
5. ๐Ÿ”„ Open a Pull Request

## ๐Ÿ“‹ Requirements

- โœ… Unity 2022.3 LTS+
- โœ… TextMeshPro
- โœ… Unity Netcode for GameObjects (optional)
- โœ… C# 9.0+ support

## ๐Ÿ“ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ™ Acknowledgments

- ๐ŸŽฎ Unity Technologies for the excellent game engine
- ๐Ÿ‘ฅ Game development community for inspiration and best practices
- ๐Ÿ“š Clean Code principles by Robert C. Martin

## ๐Ÿ“ž Support

- ๐Ÿ› **Bug Reports**: Open an issue with detailed reproduction steps
- ๐Ÿ’ก **Feature Requests**: Open an issue with [FEATURE REQUEST] prefix
- ๐Ÿ’ฌ **Questions**: Use GitHub Discussions for general questions

---

โญ **Star this repository if you find it helpful!** โญ

Made with โค๏ธ for the Unity community