Demo5 Containers
Examples/Demo5 Containers/Containers Demo.unity
This is an example of inventory-items that contain their own nested item set.
What The Demo Shows
- item instances instead of simple list rows
- a container as a regular item inside the player inventory
- a separate UI panel for the active container contents
- context menu action for opening a container
- dropping an item directly onto a container icon to put it inside
- protection against cycles when nesting containers
How It Is Structured
Data:
Scripts/Data/ItemInstance.csScripts/Data/ContainerItemInstance.csScripts/Data/IContainerizeItemInstance.csScripts/ContainerDemoManager.cs
Bindings and UI:
Scripts/Bindings/PlayerContainerInventoryDataBinding.csScripts/Bindings/ContainerInventoryDataBinding.csScripts/UI/ContainerUIController.csScripts/ContextMenu/OpenContainerMenuEntrySO.cs
How It Works
Opening a container:
ContainerItemInstanceis stored in the player inventory.- The context menu calls
OpenContainerMenuEntrySO. - Through
Events.OnOpenClick, the selected container is passed toContainerUIController. - The controller sets the active container.
ContainerInventoryDataBindingrebuilds inventory size and loads container contents.
Moving items:
- Regular drag/drop operations work between player inventory and container inventory.
- Bindings synchronize transfer into the player list or
ContainerItemInstance.Items. - If an item is dropped onto a slot that contains a container,
IPreRuleOccupiedSlotDropHandlerintercepts that attempt before regular drop rules. - The binding checks that the container has capacity and that the move would not place a container into itself or one of its children.
- If the check passes,
ContainerViewRegistry.InsertIntoContainer(...)puts the item inside the container and returnsOccupiedSlotDropResult.Handled. - If the check fails, the handler returns
Rejected, and the item stays in its original place.
When To Use This Example
- an item must contain a nested inventory
- you need a context menu for item operations
- dropping onto an already occupied slot should mean a separate action, such as "put this inside the container"