Skip to content

Tutorials · Interfaces · Chapter 2

The HUD

25 minutes Uses: Rect, Panel, Text, Stack, LayoutElement, ContentSizeFitter, AspectRatioFitter, Slider

By the end

  • A portrait that stays square, portrait
  • Three counters in a row, counters, the gem counter’s plate growing with its digits
  • A health bar, health_bar, that shows and cannot be dragged
On this page

A portrait that stays square

The plate’s lower left holds a portrait of the hero in a frame. The frame has to stay square whatever box the layout hands it, and an Aspect Ratio Fitter holds that. The frame’s picture is one more file from UI Pack: RPG Expansion.

  1. Copy the file below into art, then press Refresh in the bar of the Assets panel.
  1. Select hud_plate, click + Add node and choose Aspect Rect. Rename the node portrait.
  2. In its Rect, set Anchor Max to 0, 1, Offset Min to 8, 48, Offset Max to 80, -8 and Pivot to 0, 0. Anchor Min stays 0, 0.
  3. In its Aspect Ratio Fitter, set Ratio to 1, Mode to FromWidth and Anchor to 0, 0.
  4. Click Add component and choose PanelComponent. Set Color to 255, 255, 255, alpha 255, Texture to panelInset_brown.png and Border to 6.
  5. Select portrait, click + Add node and choose Image. Rename the node portrait_hero.
  6. In its Rect, set Anchor Min and Anchor Max both to 0.5, 0.5, Offset Min to -24, -24 and Offset Max to 24, 24.
  7. In its Panel, set Color to 255, 255, 255, alpha 255 and Texture to tilemap_packed.png. Set UV Rect to 0.166667, 0.727273, 0.25 and 0.818182.

An Image is a Panel with a Layout Element beside it. Only a Stack reads a Layout Element, and this picture’s parent is not one, so leave it as it comes.

portrait selected, its Inspector narrowed with the search box, aspect, to its Aspect Ratio Fitter: Ratio 1.00, Mode FromWidth, Anchor 0.0 and 0.0; the Viewport shows the room through the game camera with the HUD’s plate at its top left, holding the title strip, the hero’s portrait, an orange bar and three small counters
UV Rect
The part of the picture a Panel draws, as four numbers: the left and top edges, then the right and bottom, each a fraction of the picture, 0 to 1. The sheet is 192 by 176 pixels and the hero is tile 98, the 16 by 16 pixels from 32, 128 to 48, 144. Divide by the sheet’s size, 192 across and 176 down, and the fractions are 0.166667, 0.727273, 0.25 and 0.818182. A new Panel draws the whole picture, 0, 0, 1, 1. Chapter 3 names a part of a picture instead.
Ratio
The Aspect Ratio Fitter’s shape: width divided by height. 1 is a square; a new fitter’s is 1.7778, 16 by 9.
Mode
Which side the fitter keeps. FromWidth keeps the width the layout gave it and works the height out from it, FromHeight the other way round. Fit makes the largest box of the ratio that fits inside the rect, with space left over, and Cover the smallest box that covers it, cropped. A new fitter’s is Fit; the portrait takes FromWidth.
Anchor
The Aspect Ratio Fitter’s Anchor: the point of the box that stays put as the fitter reshapes it, from 0, 0 its top left to 1, 1 its bottom right. The portrait keeps its top left corner: 0, 0.

Here the anchors already give the portrait a box 72 across and 72 down, so the fitter has nothing to correct. Make the plate taller and the portrait would stay 72 square, because its height now follows its width.

Counters in a row

Three counters sit in a row under the bar: a key, a coin and a gem, each an icon and a number. A Stack lays them out, so you place the row once and not each counter.

  1. Select hud_plate, click + Add node and choose Stack. Rename the node counters.
  2. In its Rect, set Anchor Min to 0, 1, Offset Min to 88, -48, Offset Max to -12, -8 and Pivot to 0.5, 1. Anchor Max stays 1, 1.
  3. In its Stack, set Axis to Horizontal, Fit to Elements, Spacing to 12 and Padding to 4.
  4. Select counters, click + Add node and choose Node. Rename it key_counter. Add two more nodes under counters the same way, coin_counter and gem_counter. They start empty: each only groups an icon and a number.
  5. Select key_counter, click + Add node and choose Image. Rename the node key_icon. In its Rect, set Anchor Min and Anchor Max both to 0, 0.5, Offset Min to 0, -16, Offset Max to 32, 16 and Pivot to 0, 0.5. In its Panel, set Color to 255, 255, 255, alpha 255 and Texture to key.png, which came with the starter.
  6. Select key_counter, click + Add node and choose Label. Rename the node key_count. In its Rect, set Offset Min to 34, 0. In its Text, set Text to 0, Scale to 0.5 and Color to 236, 227, 206, alpha 255.
  7. Do the same under coin_counter: coin_icon, with Texture coin.png, and coin_count.
  8. Under gem_counter, add gem_icon the same way, with Texture gem_green.png. Its count comes in the next section.
counters selected, its Inspector narrowed with the search box, stack, to its Stack: Axis Horizontal, Fit Elements, Spacing 12.00, Padding 4.00, Item 48.00 and Align Start; the Viewport shows the HUD’s plate with a key, a coin and a gem in a row under the orange bar
Axis
Vertical or Horizontal: which way the Stack lays its children out. A new Stack is Vertical; the counters run Horizontal.
Fit
How the Stack shares out its length. Even gives every child an equal share. Fixed gives each child the same Item length. Elements gives each child the size its own Layout Element asks for, and a child with none asks for 48. A new Stack is Even; the counters take Elements, so the gem counter can be wider than the others.
Spacing
The gap between two neighbours, in UI units: 12. A new Stack’s is 8.
Padding
A Stack’s Padding: the margin kept inside its own edge on every side, in UI units: 4. A new Stack’s is 0.

The Stack’s other two rows, Item and Align, stay as they come. Chapter 5 sets them.

Close up, the counters with Fit Even: the key, the coin and the gem counters spread over the row in three equal shares, the coin and the gem further to the right than in the finished HUD
Close up, for comparison, Fit Even: three equal shares of the row.

A counter that grows

A gem counter can read 10, or 100000, and its number sits on a small beige plate. The plate should be as wide as its digits and no wider, so the plate measures its words and sizes itself to them: a Content Size Fitter. The counter that holds it asks the Stack for room to grow with a Layout Element.

  1. Select gem_counter, click Add component and choose LayoutElement. Set Min to 48 and Flexible to 1. Preferred stays 48.
  2. Select gem_counter, click + Add node and choose Panel. Rename the node gem_plate.
  3. In its Rect, set Anchor Min and Anchor Max both to 0, 0.5, Offset Min to 36, -11, Offset Max to 60, 11 and Pivot to 0, 0.5.
  4. In its Panel, set Color to 255, 255, 255, alpha 255, Texture to panelInset_beige.png and Border to 6.
  5. Click Add component and choose TextComponent. Set Text to 0, Scale to 0.5, Color to 63, 38, 49, alpha 255, a dark plum, and Align to Center.
  6. Click Add component and choose ContentSizeFitter. Set Horizontal to Preferred and Vertical to Unconstrained, Padding to 5, Min to 24 and 0, Max to 96 and 0, and Anchor to 0, 0.5.
gem_counter selected, its Inspector narrowed with the search box, layout, to its Layout Element: Min 48.00, Preferred 48.00 and Flexible 1.00; the Viewport shows the HUD’s plate with its three counters
gem_plate selected, its Inspector narrowed to its Content Size Fitter: Horizontal Preferred, Vertical Unconstrained, Padding 5.00, Min 24.0 and 0.0, Max 96.0 and 0.0, Anchor 0.0 and 0.5; the Viewport shows the same HUD
Min
A Layout Element’s Min: the least room a Stack gives the counter, in UI units, even if the row overflows. 48. A new Layout Element’s is 0.
Flexible
A Layout Element’s Flexible: its share of the room left in the row once every child has the size it asks for, relative to its siblings’ shares. 0, as a new one has it, never grows. The gem counter is the only child with a share, so it takes all that is left: 1.
Horizontal, Vertical
A Content Size Fitter’s two sides. Preferred sizes that side to the Text plus the padding; Unconstrained leaves it to the Rect. The plate’s width follows its digits and its height stays what its Rect says: Preferred across, Unconstrained down.
Padding
A Content Size Fitter’s Padding: the room kept round the words on every side, in UI units, with the Text laid out inside it: 5. A new fitter’s is 8.
Min
A Content Size Fitter’s Min: the smallest the plate may get, across and then down, in UI units: 24, 0. A 0 means no least on that side.
Max
A Content Size Fitter’s Max: the largest the plate may get, across and then down: 96, 0. A 0 means no limit on that side, so only the width is held, and the plate cannot outgrow its counter.
Anchor
A Content Size Fitter’s Anchor: the point of the plate that stays put as it grows, from 0, 0 its top left to 1, 1 its bottom right. 0, 0.5 is the middle of its left edge, so the plate grows to the right.

The row works out like this. Inside its padding the row is 260 units wide. The two gaps take 24, and the key and the coin counters ask for 48 each. The gem counter asks for 48 too, and its Flexible 1 gives it everything left: 140. The plate starts 36 in and may be 96 wide, so it ends inside its counter.

The pictures below show the plate reading 0, which is as you built it, and then 10 and 100000, typed into a copy of the project so you can see it grow. Nothing counts into it until chapter 7.

Close up, the HUD’s plate: the title strip with THE DELVE, the hero’s portrait in an inset frame at the lower left, a full orange bar to its right, and under the bar a key, a coin and a green gem, each with a 0 after it, the gem’s on a small beige plate
Close up: the HUD so far.
Close up, the same HUD with the gem’s count at 10: its beige plate wider than at 0, the two digits filling it
Close up, for comparison, the count at 10: the plate wider.
Close up, the same HUD with the gem’s count at 100000: the beige plate much wider, holding all six digits, and still ending well short of the HUD’s right edge
Close up, for comparison, 100000: wider again, inside its Max of 96.

A bar that only shows

The health bar is a Slider that cannot be moved. A slider and a bar are the same picture drawn from a number; Interactive is the difference. Its two pictures, bar_back.png and bar_fill.png, came with the starter.

  1. Select hud_plate, click + Add node and choose Slider. Rename the node health_bar.
  2. In its Rect, set Anchor Min to 0, 0, Anchor Max to 1, 0, Offset Min to 88, 52, Offset Max to -12, 70 and Pivot to 0, 0.5.
  3. In its Slider, set Max to 10, then Value to 10. Untick Interactive. Set Fill and Track both to 255, 255, 255, alpha 255.
  4. Under Textures, set Fill Texture to bar_fill.png, Track Texture to bar_back.png and Border to 9.
health_bar selected, its Inspector narrowed with the search box, slider, to its Slider: Action empty, Value 10.00, Min 0.00, Max 10.00, Step 0.00, Interactive unticked, Fill and Track white, then under Textures Fill Texture bar_fill.png, Track Texture bar_back.png and Border 9.00; the Viewport shows the HUD’s plate with the orange bar full
Value, Max
A Slider’s Value is where the bar stands, and its Max is where it is full: the bar is filled Value out of Max. A new Slider’s Max is 1. Health is counted out of 10, so both are 10. Set Max first, so that Value is never past it.
Interactive
Whether the pointer and the keys can move the bar. Unticked, it is a gauge: a picture of a number that nothing can drag. An interactive Slider with no Action shows a line in Problems; a gauge does not, since nothing is meant to hear it.
Fill, Track
Tints for the filled part and for the empty track. A tint multiplies its picture, so white draws each picture as it is. A new Slider’s are grey and dark.
Fill Texture, Track Texture
The pictures drawn over the filled part and over the whole track. Empty, the bar is a flat colour.
Border
A Slider’s Border: the nine-slice border both its pictures share, in the pictures’ own pixels. The bar art is 36 by 18 with round ends 9 pixels wide, so 9 keeps the ends round while the middle stretches to any length.

The Slider’s other rows stay as they come: Action and Step are set in chapter 6, and Min in chapter 16.

The portrait, the counters and the health bar

The portrait

NodeComponentFieldValue
portraitRectAnchor Min0, 0
Anchor Max0, 1
Offset Min8, 48
Offset Max80, -8
Pivot0, 0
PanelColor255, 255, 255, alpha 255
TexturepanelInset_brown.png
Border6
Aspect Ratio FitterRatio1
ModeFromWidth
Anchor0, 0
portrait_heroRectAnchor Min0.5, 0.5
Anchor Max0.5, 0.5
Offset Min-24, -24
Offset Max24, 24
PanelColor255, 255, 255, alpha 255
Texturetilemap_packed.png
UV Rect0.166667, 0.727273, 0.25, 0.818182

The counters

NodeComponentFieldValue
countersRectAnchor Min0, 1
Offset Min88, -48
Offset Max-12, -8
Pivot0.5, 1
StackAxisHorizontal
FitElements
Spacing12
Padding4
key_counter, coin_counter, gem_counterempty nodes
key_icon, coin_icon, gem_iconRectAnchor Min0, 0.5
Anchor Max0, 0.5
Offset Min0, -16
Offset Max32, 16
Pivot0, 0.5
PanelColor255, 255, 255, alpha 255
Texturekey.png, coin.png, gem_green.png
key_count, coin_countRectOffset Min34, 0
TextText0
Scale0.5
Color236, 227, 206, alpha 255

The gem counter

NodeComponentFieldValue
gem_counterLayout ElementMin48
Flexible1
gem_plateRectAnchor Min0, 0.5
Anchor Max0, 0.5
Offset Min36, -11
Offset Max60, 11
Pivot0, 0.5
PanelColor255, 255, 255, alpha 255
TexturepanelInset_beige.png
Border6
TextText0
Scale0.5
Color63, 38, 49, alpha 255
AlignCenter
Content Size FitterHorizontalPreferred
VerticalUnconstrained
Padding5
Min24, 0
Max96, 0
Anchor0, 0.5

The health bar

ComponentFieldValue
RectAnchor Min0, 0
Anchor Max1, 0
Offset Min88, 52
Offset Max-12, 70
Pivot0, 0.5
SliderValue10
Max10
Interactiveunticked
Fill255, 255, 255, alpha 255
Track255, 255, 255, alpha 255
Fill Texturebar_fill.png
Track Texturebar_back.png
Border9

Every other field of every node is as it comes.

Check it works