UI Toolbox logoUI Toolbox
Support

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.

A pointy-top HexGrid skill board with a selected cell, and a flat-top HexGrid mapA pointy-top HexGrid skill board with a selected cell, and a flat-top HexGrid map

<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 d-pad, the left stick and the arrow keys move the focus to the nearest neighbor in the direction of the press.

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.