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.
- Host: GitHub
- URL: https://github.com/pietras333/inventory-system
- Owner: pietras333
- Created: 2025-07-05T15:22:28.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-05T15:38:43.000Z (12 months ago)
- Last Synced: 2025-07-05T16:37:38.574Z (12 months ago)
- Topics: csharp, gamedev, inventory-system, unity3d
- Language: C#
- Homepage:
- Size: 35.2 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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