ItemSlot
ItemSlot in UIToolbox is one slot of a bag, a stash or a set of equipment: the icon of an item, the size of its stack, a border in the color of its rarity, a durability bar, a lock and a cooldown sweep with the seconds left. Set Item to show an item of your game, or set the fields one by one. An InventoryGrid is made of these slots and moves the items between them. Use a slot alone for a shop row, a reward, a crafting result or the item of a details window.


Video tutorial: Inventory grid. All videos: Video tutorials. Demo scene: ItemSlotDemo.unity.
<tb:ItemSlot
name="potion"
count="5"
max-count="20"
rarity="Rare"
durability="0.6" />
var slot = root.Q<ItemSlot>("potion");
slot.Icon = Background.FromTexture2D(potionIcon);
slot.CooldownFinished += () => status.text = "Ready.";
// A drink: one potion less, and the potions cool down for 6 seconds
slot.Count--;
slot.StartCooldown(6);
How to show an item of your game: implement IItemSlotData on its class, or use ItemStack, and set Item.
var potion =
new ItemStack("Health Potion", potionIcon, count: 5, maxStack: 20,
rarity: "Common");
slot.Item = potion;
// The slot copies the icon, the count, the stack size and the rarity:
// set Item again after the item changes
potion.Count--;
slot.Item = potion;
// Null empties the slot
slot.Item = null;
IItemSlotData has five members: Icon (a Texture2D or null), an optional VectorIcon (an SVG; the default is none), Count, MaxStack, Rarity (a text or null) and Size, the cells that the item takes in a shaped InventoryGrid (1 x 1 for most items).
Settings
| Setting (UXML) | Type | Default | Does |
|---|---|---|---|
icon |
Background |
none | The picture of the item: a Texture2D, a Sprite or an SVG (VectorImage). An ItemStack has Icon and VectorIcon. |
empty-icon |
Background |
none | Shows as a dark silhouette when there is no icon, such as the outline of a helm in a head slot. --tb-item-slot-empty-color sets its color. |
count |
int | 0 | The size of the stack, at the bottom right. |
max-count |
int | 0 | The most items of one stack. The count shows when this or count is over 1, so one potion of a stack of 20 shows "1" and a sword shows no count. |
rarity |
string | empty | The rarity, such as Common, Uncommon, Rare, Epic or Legendary. It adds the class tb-item-slot--{rarity}, in lower case with dashes for spaces, which colors the border. Any other text works with a class of your own. |
durability |
float | -1 | From 0 to 1, as a bar at the bottom. Under 0 hides the bar. At a quarter or less the bar turns red, with the class tb-item-slot--worn. |
locked |
bool | false | Shows a lock over a dark slot, for a slot the player has not opened yet. An InventoryGrid moves nothing in or out of a locked slot. |
show-seconds |
bool | true | Shows the seconds left in the center while the slot cools down. |
unscaled-time |
bool | false | The cooldown runs on unscaled time, so it runs on in a pause or a slow motion. |
Set these from code:
| Property | Does |
|---|---|
Item |
The IItemSlotData that the slot shows, or null for an empty slot. A set copies its icon, count, stack size and rarity into the slot. |
Rotated |
Turns the icon a quarter, for an item that lies turned in a shaped InventoryGrid. The grid sets it. |
IsCoolingDown |
True while a cooldown runs (read only). |
CooldownRemaining |
The seconds left of the cooldown, or 0 (read only). |
CooldownProgress |
From 0 at the start of a cooldown to 1 when it is over (read only). |
Methods
| Method | Does |
|---|---|
StartCooldown(seconds) |
Starts a cooldown, such as the shared cooldown of the potions. The same as SetCooldown(seconds, seconds). |
SetCooldown(remaining, total) |
Starts a cooldown part way, for a load, a server value or a slot that shows again after a scroll. 0 or less, or NaN, clears the cooldown. |
ClearCooldown() |
Ends the cooldown now, with no CooldownFinished. |
Events
| Event | When |
|---|---|
CooldownFinished |
A cooldown runs out. ClearCooldown and a new cooldown of 0 do not raise it. |
Mouse, keyboard and gamepad
The slot takes the focus, and its border shows the accent color under the pointer and while it has the focus. It has no actions of its own: an InventoryGrid adds the drag, the split and the keys. For a slot alone, add a Clickable, a DragSource or a ToolTip to it.
Performance
- One ticker per panel steps all the cooling slots, the same ticker as the AbilitySlots. A slot that does not cool down costs nothing per frame.
- A new count, rarity, durability, lock or cooldown allocates nothing after the first use: the counts up to 99 and the seconds come from tables, a count over 99 makes one string for each new value, and each rarity makes its class once.
In the examples



Styling
| Class | Element |
|---|---|
tb-item-slot |
The slot: 56 px wide and high, with a 2 px border. The border shows the accent color under the pointer and with the focus. |
tb-item-slot__icon |
The icon (a MaskImage). |
tb-item-slot__sweep |
The dark cooldown sweep. |
tb-item-slot__seconds |
The seconds left. |
tb-item-slot__count |
The size of the stack. |
tb-item-slot__durability, __durability-fill |
The durability bar and its fill: green, and red when worn. |
tb-item-slot__lock |
The lock and the dark cover of a locked slot. |
tb-item-slot--empty |
No item and no icon. |
tb-item-slot--locked |
Locked is true. |
tb-item-slot--cooling |
During a cooldown. |
tb-item-slot--worn |
The durability is a quarter or less. |
tb-item-slot--common, --uncommon, --rare, --epic, --legendary |
The border color of each rarity, from the theme variables --tb-rarity-common to --tb-rarity-legendary. |
| Variable | Default | Does |
|---|---|---|
--tb-item-slot-empty-color |
rgba(0, 0, 0, 0.5) |
The tint of the empty icon. |
--tb-item-slot-sweep-color |
rgba(0, 0, 0, 0.6) |
The color of the sweep. |
--tb-rarity-common, -uncommon, -rare, -epic, -legendary |
gray, green, blue, purple, orange | The border colors of the rarities. A theme sets them; see Theme variables. |
Add a rarity of your own with a class of the same form, and change the size and the sweep of the slots in one window:
.tb-item-slot.tb-item-slot--mythic {
border-color: rgb(230, 204, 128);
}
.my-shop .tb-item-slot {
width: 64px;
height: 64px;
--tb-item-slot-sweep-color: rgba(20, 0, 40, 0.7);
}
Inside an InventoryGrid, the slot-size of the grid sets the width and height of the slots.
The ItemSlotDemo scene shows a large slot with fields for each part and the UXML that follows them, a slot for each state with its class, a belt of potions and bombs that Q and E or the shoulder buttons pick and F or the West button uses, a drag between a bag and the Weapon and Off hand slots that also works with Enter or South, and the looks: shapes, a size, a rarity of your own and the colors of the sweep and of the empty icon. See also InventoryGrid, AbilitySlot and Hotbar.