This repository contains a Unity Inventory System designed for handling item storage, interactions (e.g., moving, swapping, merging items), and visualizing states efficiently in Unity-based projects. It includes core functionality, test modules, and the foundational structure for extending inventory systems in Unity.
Here’s an overview of the project:
.
├── Core # Core logic, system operations, and data management
│ ├── Data # Data models representing items and their states
│ │ ├── StoragePage.cs
│ │ ├── ItemInstance.cs
│ │ └── ItemDisplayState.cs
│ ├── Operations # Core logic for handling inventory operations
│ ├── BaseOperation.cs
│ ├── MoveOperation.cs
│ ├── SwapOperation.cs
│ └── MergeOperation.cs
├── View # UI and visualization components (future expansion)
├── Test # Unit and integration tests (pending deeper inspection)
└── Definitions # General definitions like enums and constants
This inventory system offers essential features for Unity projects:
-
Data Handling (
Core/Data)- Represents individual items (
ItemInstance.cs) with their data and states. - Manages storage pages (
StoragePage.cs) for grouping items (e.g., inventory tabs, storage grids, etc.). - Handles display states (
ItemDisplayState.cs) for visual representation.
- Represents individual items (
-
Inventory Operations (
Core/Operations)- MoveOperation: Logic for moving items within the inventory or to/from storage.
- SwapOperation: Handles swapping items between inventory slots or grids.
- MergeOperation: Implements merging logic for items (e.g., stacking or combining).
- BaseOperation: A shared foundation enabling easy extension by inheriting common behavior.
-
Scalable Architecture
- Abstraction-centric design ensures that developers can add new operations (drag-and-drop, split-stack, craft recipes, etc.) by extending the
BaseOperationclass. - Separation of concerns ensures that data handling and operations are decoupled from UI for clarity and flexibility.
- Abstraction-centric design ensures that developers can add new operations (drag-and-drop, split-stack, craft recipes, etc.) by extending the
-
Test-Driven Development
- Robust test coverage (in the
Testfolder) ensures core functionality like item state changes, movement, and swapping works as expected.
- Robust test coverage (in the
-
Unity Version: Ensure your Unity Editor build is compatible with
.csfiles in the repository. -
Environment Requirements:
- Tested on Unity (Specify version recommendation, e.g., 2023.X.X or higher).
- Optional: .NET runtime version for advanced scripting.
-
IDE: Use JetBrains Rider, Visual Studio, or any Unity-compatible code editor.
- Open your Unity project and clone the repository directly into your
Assetsfolder.
cd <your-unity-project-path>/Assets
git clone <repository-url>-
Alternatively, manually copy the folder structure into your
Assets/InventorySystemdirectory. -
Configure Inventory Manager:
- Create a new empty GameObject in your Unity scene.
- Add a script/component (not explored yet; assume
InventoryManageras placeholder name) to manage inventory actions and tie them to UI or gameplay events.
-
If testing is required, ensure the Unity Test Framework is enabled:
- From Window → Package Manager, install or enable
Test Framework.
- From Window → Package Manager, install or enable
Run tests via:
Unity Test RunnerThe design of this Unity inventory system follows best practices for maintainable systems:
-
StoragePage.cs
-
Abstraction of inventory "pages" or "containers", such as:
- Backpack tabs
- Storage chests
- Hotbar groups
-
Ensures flexibility by supporting any container-based inventory system.
-
Maintains modularity to adapt to different types of inventories.
-
-
ItemInstance.cs
- Represents individual items in the inventory.
- Designed to store item metadata, including unique ID, display name, and possible item-specific properties (weights, rarity, stats, etc.).
- Keeps data storage efficient, avoiding responsibility overlaps with UI code.
-
ItemDisplayState.cs
- Designed to handle visual representations (selected, highlighted, cooldown overlays, etc.) easily.
- By separating display logic from data, the system avoids tightly coupling UI changes with backend changes.
- Enables designers and coders to collaborate seamlessly by interacting with clear states.
-
BaseOperation.cs
- A parent class with reusable logic common to all inventory-related operations. Adheres to the DRY Principle by reducing repeated code across specific operations (e.g., input checks, result validation).
-
MoveOperation.cs
- Focused entirely on relocating items.
- An intentional design, as moving is one of the most frequent actions in an inventory system.
- Extensible for additional logic (e.g., if moving an item has weight or slot dependencies).
-
SwapOperation.cs
- Simplifies swapping items between slots (or pages).
- Follows Single Responsibility Principle, ensuring no code outside of this class performs swapping logic.
-
MergeOperation.cs
- Handles complex behaviors like stacking or merging items (e.g., ores, resources).
- Preserves extensibility for behaviors like rules-based merges (e.g., same item type, compatible modifiers).
- Create a new
.csfile inCore/Operations/. - Inherit from
BaseOperation. - Override required methods:
public override void Execute(ItemInstance source, ItemInstance target) {
// Define your operation logic here
}Simply extend the ItemInstance class or add new item properties to support traits or behaviors like durability, rarity, or stack limits.