A high-performance Unity 2D grid inventory system with drag-and-drop support, item rotation, and stacking.
- 2D Grid-Based Inventory: Efficient grid system using span-based bit arrays and unsafe pointers
- Drag & Drop: Intuitive item dragging between multiple inventories with visual feedback
- Item Rotation: Press R while dragging to rotate items (90°/180°/270°)
- Collision Detection: Real-time placement validation using shape overlap checks
- Object Pooling: Minimized GC allocations through pooling of shapes and UI elements
- uGUI Integration: Ready-to-use Unity UI components
- Unity 2022.3 or later
Open Assets/Scenes/Inventory(uGUI).unity to see a working example.
This repository contains two Unity packages:
Low-level grid shape library providing:
IGridShape<T>: Interface for mutable grid shapes with typed cell valuesImmutableGridShape: Memory-efficient readonly grid shapes using bit arraysIndexedGridBoard: Grid container managing multiple items with collision detection- Grid transformations (rotation, flipping, trimming)
uGUI-based inventory system built on DopeGrid:
IGameInventory/IUIInventory: Inventory model interfaces with GUID-based item trackingInventoryView: Main MonoBehaviour for inventory UIInventoryViewDragController: Drag-and-drop interaction handlingInventoryItemViewPool: Object pooling for UI elements
InventoryView (Packages/com.fullmetalbagel.dope-inventory-ugui/Runtime/UI/InventoryView.cs)
Main component orchestrating inventory UI, handling user input, and synchronizing model-view state.
IGameInventory (Packages/com.fullmetalbagel.dope-inventory-ugui/Runtime/Model/IGameInventory.cs)
Core inventory model interface managing item placement, movement, and removal with collision detection.
IndexedGridBoard (Packages/com.fullmetalbagel.dope-grid/Runtime/IndexedGridBoard.cs)
Grid container storing item IDs per cell, providing efficient spatial queries and collision checking.
Unity Tests:
# Open Test Runner in Unity Editor
# Window > General > Test Runner
# Click "Run All" for EditMode or PlayMode tests.NET Tests (faster, no Unity dependency):
cd dotnet
dotnet test DopeGrid.sln
# With coverage
dotnet test --collect:"XPlat Code Coverage"
# Specific test
dotnet test --filter "FullyQualifiedName~TestName"- C# 10+ with nullable reference types enabled
- Unsafe code and pointers for performance-critical grid operations
- Pure functions marked with
[Pure, MustUseReturnValue] - Strict analyzer rules with
TreatWarningsAsErrors
Packages/
├── com.fullmetalbagel.dope-grid/ # Low-level grid library
│ ├── Runtime/ # Grid shapes, boards, transformations
│ └── Editor/ # Editor utilities
├── com.fullmetalbagel.dope-inventory-ugui/ # Inventory UI system
│ ├── Runtime/
│ │ ├── Model/ # Inventory model layer
│ │ ├── UI/ # UI components
│ │ └── Input/ # Input abstraction
Assets/
├── Scenes/ # Demo scenes
└── Tests/ # Unity test cases
dotnet/
└── DopeGrid/ # Standalone .NET project (same source)
MIT License - see LICENSE for details.