HexGrid
HexGrid in UIToolbox is a layout container that puts its children on a honeycomb of hexagons: a skill board, a strategy map, a tile picker. It lays out in C#, so it works in each Unity 6 version, and it makes no garbage after the first layout. The d-pad moves the focus to the nearest neighbor, and A selects a cell.


<tb:HexGrid name="board" columns="5" cell-size="72" gap="4"
draw-cells="true">
<ui:Button text="1" />
<ui:Button text="2" />
<ui:Button text="3" />
</tb:HexGrid>
<tb:HexGrid orientation="FlatTop" columns="0" cell-size="64" />
var grid = new HexGrid { Columns = 5, CellSize = 72, Gap = 4 };
for (var i = 0; i < 12; i++) grid.Add(new Button());
grid.SelectionChanged += index => Debug.Log(index);
// A cell by its column and row, and its neighbors
var cell = grid.CellAt(2, 1);
Span<int> next = stackalloc int[6];
var count = grid.Neighbors(7, next);
The children fill the grid row by row (PointyTop) or column by column (FlatTop), one child for each cell. In PointyTop the cells have a point at the top and the odd rows sit half a cell to the right. In FlatTop the cells have a flat top and the odd columns sit half a cell down.
| Member | Description |
|---|---|
Orientation (orientation) |
PointyTop (the default) or FlatTop. |
Columns (columns) |
The cells in a row. 0 (the default) fits as many as the width allows. |
CellSize (cell-size) |
The width of a cell from flat side to flat side, in pixels. 64 by default. |
Gap (gap) |
The space between two cells in pixels. 4 by default. |
DrawCells (draw-cells) |
Draws a hexagon behind each child instead of the rectangle of the button. |
SelectedIndex, SelectedCell |
The selected cell, or -1 and null for none. Setting the index raises SelectionChanged. |
SelectionChanged |
An event with the new index. A, Enter and a click select a cell. |
CellCount, EffectiveColumns, RowCount |
The cells with a spot, the columns in use and the rows. |
CellAt(column, row), IndexOf(column, row) |
The cell at a spot and its index, or null and -1 outside the grid. |
ColumnOf(index), RowOf(index), CellOf(index) |
The column, the row and the element of an index. |
CellCenter(column, row), CellBox, ContentSize |
The center of a spot, the size of one cell box and the size of the whole honeycomb. |
Neighbors(index, Span<int>) |
Writes the indices of the neighbors of a cell and returns how many. A cell has up to 6, fewer at an edge or a corner. A span shorter than 6 gets the first ones. |
Refresh() |
Lays out at once. The layout runs by itself when its width, its settings or its children change. |
- The children are positioned absolutely. Give the grid a width. Its height grows to hold the rows.
- A child with
display: nonetakes no spot. Show it again and the others make room. - With
Columnsat 0 the grid counts the columns that fit in its width and lays out again when the width changes.
Navigation
The d-pad, the left stick and the arrow keys move the focus to the nearest neighbor in the direction of the press.
- In
PointyTop, Left and Right move along the row. Up and Down zigzag: every cell has two neighbors above and two below, so the press picks the one that is nearer to the place where the run of presses began. When both are as near, the left one wins. - In
FlatTop, Up and Down move along the column. Left and Right zigzag in the same way, and a tie goes to the upper cell. - A press with no cell in that direction is not handled, so the focus leaves the grid and never gets stuck in it.
Styling
| Class | Element |
|---|---|
tb-hex-grid |
The grid. It sets the custom properties --tb-hex-fill, --tb-hex-border, --tb-hex-selected, --tb-hex-focus and --tb-hex-hover. |
tb-hex-grid--cells |
The grid while DrawCells is on. The buttons lose their own look. |
tb-hex-grid__cell |
Each child. |
tb-hex-grid__cell--selected |
The selected child. |