Inventory
Inventory is the item screen of a role-playing game, in two forms: a bag with equipment slots, and a backpack and a chest where items have different sizes. Each grid is an InventoryGrid of ItemSlots. The grids do the drag and drop, the split of a stack, the keyboard and the gamepad; the example gives them the items and the rules. Each item has a tooltip. The three windows sit in one workspace. A Style field in the toolbar changes the look of all of them with one class. The "How it is made" foldout uses a NavigableTabView and CodeBlock controls.

Scene: Assets/UIToolbox/Examples/Scenes/Inventory.unity. Folder: Assets/UIToolbox/Examples/Inventory.
Try it
- Open the scene. Three windows are open: Inventory, Item and Shaped items. The toolbar shows the gold, the value of the items and the weight.
- Drag the Iron Helm from the bag onto the Head slot. The helm moves into the slot. Drag the Moonblade onto the Head slot. The slot denies the drop, because it takes only a head item, and the status line tells why.
- Drag one stack of Health Potions onto the other. The stacks merge up to 10, and the rest stays in the first slot.
- Right click the Iron Ore. Half of the stack goes to the first empty bag slot. Hold Shift while you drag a stack, and half of it moves.
- Press Sort bag. The bag items go to the first slots, by kind, then by rarity, then by name.
- Click a slot. The Item window shows the item. Press Use to drink a potion, equip a piece or unequip it. Press Sell to get gold for the whole stack.
- Click the Iron Helm. Over the buttons, a compare shows its stats against the Head slot, which is empty. Click the Moonblade: it compares with the equipped Short Sword, with the better numbers in green. Click a potion: it fits no slot, so the compare hides. Press Equip on the Dragonscale Mail, and the compare turns around: the Leather Armor in the bag against the mail you wear.
- Move the pointer over an item. A tooltip shows the name, the rarity, the description, the value and the weight.
- Use the arrow keys or the D-pad on a slot. The focus moves through the bag, and Left from its first column goes to the equipment. Press Enter or the south button. The slot is now held. Move to another slot and press Enter again to put the item there. Escape or the east button puts it back.
- In the Shaped items window, drag the Tower Shield over the backpack. The cells under it turn green when the whole item fits there, and red when it does not. Release to drop it.
- While you drag, press R or the right mouse button. The item turns, so its width and its height change places, and the left button keeps the drag. Press again to turn it back.
- Click an item and press Rotate. The item turns in its place when it has room.
- Find the Sun Cross, a plus, and the Seven-League Boot, an L, at the bottom of the backpack. Only their own cells take room: drag a potion into the empty corner of the L, and it fits there. A click in that corner selects the potion, not the boot. Drag the boot and press R: its cells turn with it.
- Press Arrange. The backpack items go again from the top left, the largest first.
- Focus an item with Tab, press Enter, move it with the arrows, and press Enter to put it down. The two grids share the held item, so it can go down in the chest too.
- Open the Style field and choose Sci-fi or Parchment. All the windows change at once.
- Open the "How it is made" foldout and its tabs. The last line of each C# block follows what you do.
- Press Start challenge. Pack at least 4,000 gold of cargo and a Health Potion in the chest, with no more than 8 weight. Press Depart to check the load. Restart challenge restores the starting cargo. Free play restores your earlier arrangement.
What it shows
Bag and equipment
Two InventoryGrids on one model: a column of four equipment slots (Head, Body, Weapon, Trinket) and a bag of 6 by 4 slots. The model has the equipment slots first, then the bag, so the bag grid starts at first-index 4, and an item drags between the two grids. A drag moves an item to an empty slot, fills a stack of the same item, or swaps two items. Shift drags half a stack, and a right click splits one. An equipment slot takes only its own kind. An empty equipment slot shows the kind it takes.
The Sort bag button sorts the bag and merges partial stacks of the same item. The equipment stays.
Item window
The window shows the selected slot: the picture in an ItemSlot, the name, the rarity, the description and the numbers. Use is named by what it does (Drink, Equip or Unequip), and a slot with no use disables it. Sell gives the value of the item times the count.
A bag item that fits an equipment slot shows an ItemCompare over the buttons, against the item in that slot, or "Nothing equipped" when the slot is empty. It has a row for each stat bonus of either item, then the value and the weight, and green and red mark the better and the worse numbers. The compare follows each selection, and hides for a potion and for an equipped item. The item, the compare and Equip read as one panel.
Toolbar totals
The toolbar shows the gold, the value of all the items and the weight as Weight x / 60. Equipped items count in both totals.
Shaped items
Two shaped InventoryGrids on one model: a backpack of 8 by 8 cells and a chest of 5 by 6 cells. An item takes a rectangle of cells, such as the 1 by 3 of the Moonblade or the 3 by 3 of the War Hammer, or the cells of its ItemShape: the Sun Cross is a plus and the Seven-League Boot an L. The free cells in the box of a shape take other items, and the pictures of the two have their shapes. A drag shows a ghost of the item, and the cells under it show whether the whole item fits. R, the right mouse button or the Rotate button turns an item. The status line tells the result of each action. The toolbar shows the cells in use for each grid.
Pack the caravan
The optional challenge gives the shaped grids a delivery goal. Its Card shows the chest value, weight and medicine. The totals read the items currently in the chest. Returning an item to the backpack removes its contribution. Depart rejects incomplete cargo and held items. Success locks the delivered load until Restart challenge or Free play.
The challenge uses a separate inventory model. Free play keeps its items and rotations. Start and Restart challenge focus the backpack for keyboard and gamepad input. The challenge buttons use native Submit. All existing drag, turn and grid navigation controls remain available.
How it is made
A NavigableTabView with three tabs: "Drag and turn", "Keyboard and gamepad" and "Look". Each tab has a CodeBlock with the UXML and a CodeBlock with the C#. The C# gets one extra last line from the current state: the last drop, the selected item, and the look class.
Three looks
Classic, Sci-fi and Parchment. Classic uses the theme. The other two are classes on the root of the example.
How it is built
| File | Does |
|---|---|
InventoryController.cs |
The Inventory and Item windows: puts the two grids on one model, and shows the pictures, the tooltips, the status line, the totals and the Item window with its compare. It also holds InventoryStyles, and Fill, which the Shaped items window and the Crypt example use too. |
SampleInventory.cs |
The data and the rules: items, slots, move, stack, swap, use, sell and sort, in a plain C# object. SampleInventoryModel gives it to the grids as an IInventoryModel. |
ShapedInventoryController.cs |
The Shaped items window: the start items, the pictures, the status line, the Rotate and Arrange buttons, the totals, the optional caravan challenge and the code blocks. |
ItemIcon.cs |
The painted picture of an item over a glow of its rarity, shown in an ItemSlot in place of an icon texture. The pictures are in Resources/UIToolboxSamples/Items and come from Tools/skins/items.mjs. The plus and the L have pictures in their shapes, and turn with the item. |
Inventory.uxml |
The workspace, the toolbar and the three windows, with the four grids and the slot of the Item window. |
Inventory.uss |
The windows, the label of an empty equipment slot, and the three looks. |
The rules live in SampleInventory, and the grids only show them. SampleInventoryModel answers CanMove and makes each Move with the rules of the inventory, so one place decides every drop of the mouse, the keyboard and the gamepad.
public bool CanMove(InventoryMove move) =>
_inventory.CanMove(SlotAt(move.From), SlotAt(move.To), move.Count);
public bool Move(InventoryMove move) =>
_inventory.Move(SlotAt(move.From), SlotAt(move.To), move.Count);
A slot takes an item only if it accepts the kind.
public bool Takes(InventoryItem item) => item == null || Accepts == null
|| Accepts == item.Kind;
The two grids share the model. The handlers come before the model, so the first bind of the slots shows the pictures and the tooltips.
_model = new SampleInventoryModel(_inventory);
_equipment = content.Q<InventoryGrid>("equipmentSlots");
_bag = content.Q<InventoryGrid>("bagSlots");
_bag.FirstIndex = _model.BagFirst;
foreach (var grid in new[] { _equipment, _bag })
{
grid.SlotBound += (slot, index) =>
fill(slot, _model.SlotAt(index), true);
grid.SelectionChanged += index =>
{
if (index >= 0) select(index);
};
grid.MoveRequested += moved;
grid.MoveDenied += denied;
grid.Model = _model;
}
content.Q<Button>("sort").clicked += _bag.Sort;
The compare of the Item window finds the slot of the kind of the selected bag item, and gives both items the same rows, so ItemCompare lines them up. With nothing in the slot, the current side has no rows and shows a dash.
var worn = _inventory.Equipment.Find(s => s.Accepts == item.Kind)?.Item;
compare.SetStats(stats(worn, item), stats(item, worn));
InventoryController.Fill puts the picture of the item in the slot, the kind in an empty equipment slot, and the name and the text in the tooltip of the slot. The slot keeps one tooltip; a new item only changes its text.
A grid moves the focus with the arrows inside it, and lets an arrow past its side go on to its parent. The bag sends Left from its first column to the equipment. IgnoreEvent stops the focus controller from moving the focus a second time.
_bag.RegisterCallback<NavigationMoveEvent>(evt =>
{
var index = _bag.IndexOf(evt.target as VisualElement) - _model
.BagFirst;
if (evt.direction == NavigationMoveEvent.Direction.Left && index >= 0
&& index % SampleInventory.COLUMNS == 0)
focus(evt, _equipment,
Mathf.Min(index / SampleInventory.COLUMNS,
_model.BagFirst - 1));
});
private static void focus(NavigationMoveEvent evt, InventoryGrid grid,
int index)
{
var slot = grid.SlotAt(index);
if (slot == null) return;
slot.Focus();
slot.focusController?.IgnoreEvent(evt);
evt.StopPropagation();
}
The shaped grids show a ListInventoryModel with one slot for each cell of the backpack, then of the chest. Each start item is an ItemStack with a Size, and the plus and the L also with a Shape, in the slot of its first cell: the top left cell of a rectangle, or the first covered cell of the top row of a shape. Its Data holds the InventoryItem of the sample, for the picture and the tooltip.
model.Set(first + y * grid.x + x,
new ItemStack(name, rarity: ((IItemSlotData)item).Rarity)
{
Description = item.TooltipText,
Size = new Vector2Int(w, h),
Shape = shape,
Data = item,
});
var boot = new ItemShape("X.", "X.", "XX");
var cross = new ItemShape(".X.", "XXX", ".X.");
_chest.FirstIndex = BACKPACK_SIZE.x * BACKPACK_SIZE.y;
foreach (var grid in new[] { _backpack, _chest })
{
var g = grid;
g.SlotBound += (slot, index) =>
InventoryController.Fill(slot,
itemAt(index)?.Data as InventoryItem);
g.SelectionChanged += index => selected(g, index);
g.MoveRequested += moved;
g.MoveDenied += move =>
_status.text = $"{itemAt(move.From)?.Name} does not fit there.";
g.Model = _model;
}
The Rotate button calls Rotate of the grid with the last selection, which turns the item in the hand, or the selected item in its place. Arrange calls Sort of the backpack, which puts the largest items first.
The look
One Style field sets the look of both controllers. Each look after Classic is one class on the root. Both controllers call Bind, so each works alone.
public static readonly string[] NAMES =
{
"Classic",
"Sci-fi",
"Parchment"
};
public static readonly string[] CLASSES =
{
null,
"inventory--scifi",
"inventory--parchment"
};
The look classes override the classes of the slots and the cells of the grids. They change only the background, the corners and the width of the borders, so the rarity colors of the slots stay. The Sci-fi look has sharp corners, thin lines and a cold ground.
.inventory--scifi .tb-item-slot,
.inventory--scifi .tb-inventory-grid__cell {
border-width: 1px;
border-radius: 0;
background-color: rgba(0, 190, 230, 0.08);
}
.inventory--scifi .tb-item-slot:hover,
.inventory--scifi .tb-inventory-grid__slot--selected {
background-color: rgba(0, 190, 230, 0.25);
}
The Classic look takes its colors from the theme tokens, so a theme change restyles it. The cells of the shaped grids are 44 px, the slot-size in Inventory.uxml and ShapedInventoryController.CELL. Change both together.
Use it in your game
- Use an
InventoryGridfor each bag, chest or set of equipment. Keep the items in a model:ListInventoryModelwithAcceptsfor simple rules, or anIInventoryModelof your own over your item data, asSampleInventoryModeldoes. KeepCanMoveas the one place that decides if a move is allowed. - Put the grids of one character on one model with
first-index, so items drag, stack and swap between them. - Show your own pictures in
SlotBound, asInventoryController.Filldoes. The Crypt example usesFillfor its bag and its gear. - Use
ToolTipwithHeaderandBodyfor the item card. The body takes rich text. - Turn on
shapedwhen items take more than one cell.FindRoomfinds the first place where an item fits, andSortpacks the largest items first. A game that must pack every set needs a packing search. - Give an item an
ItemShapewhen it is not a rectangle. A[Serializable]ItemShapefield keeps the shape in a ScriptableObject of your items. See Item shapes. - Show an
ItemComparenext to the actions of the selected item, so the player sees what Equip changes before the press. - The keyboard and the gamepad need no code: submit, cancel, the arrows and R give a gamepad player the same actions as a mouse. Add a
NavigationMoveEventcallback when the arrows go from one grid to another. - Add a look as one class on your root and write its rules in your USS. The looks of this example are sample-only.
- The start items, the gold, the weight limit of 60 and the item pictures are sample-only.