Master Invictus

Guide

How to set up a game HUD with nine-slice panels in Godot 4

4 December 2026

Use a NinePatchRect node. Give it the panel image as its texture, type the four patch margins (left, top, right, bottom) in pixels, and then size the node as large as you like. The corners keep their size, the edges stretch along their length, and the centre stretches both ways. That is nine-slice scaling, and it lets one small image serve as a panel of any size.

What the margins mean

Two vertical lines and two horizontal lines cut the image into nine pieces. The left margin is the distance from the left edge to the first vertical line, and the same goes for the other three sides. Set each margin just past the corner art on that side, so no part of a corner falls into a piece that stretches.

1. Set up one panel

  1. Add a NinePatchRect node under a Control or CanvasLayer.
  2. Drag the panel PNG onto its Texture property.
  3. Open Patch Margin in the Inspector and enter Left, Top, Right and Bottom.
  4. Resize the node in the viewport. The border should stay the same thickness at every size.

Leave Axis Stretch on Stretch for flat borders. Tile and Tile Fit repeat the edge pieces, which suits a border with a pattern along it. Switch off Draw Center for an open frame that shows the game through its middle.

2. Read the margins from a kit

A kit made for nine-slice use should list its margins, so nobody has to measure them. A list in the order left, top, right, bottom maps straight onto the four Patch Margin fields. Two examples from a 256 by 160 pixel panel set:

PieceLeftTopRightBottom
A plain panel16161616
A panel with a header bar16322816

The header panel has a larger top margin because its title bar must not stretch. If you use the double-size PNG of a piece, double every margin too.

3. Buttons

A Button draws its background from a StyleBox. Select the button, open Theme Overrides, then Styles, and make a new StyleBoxTexture for Normal. Set its Texture, then its Texture Margins, with the same four numbers. Do the same for Hover, Pressed and Disabled with the matching images. To style every button at once, put the four style boxes in a Theme resource and set that theme on the root Control.

4. Health and energy bars

Use a TextureProgressBar. Put the bar frame in Under or Over and the fill in Progress, switch on Nine Patch Stretch, and enter the Stretch Margin values. The bar can then be any length while its end caps keep their shape.

5. Keep it sharp at every window size

  • In Project Settings, under Display, Window and Stretch, set Mode to canvas_items and Aspect to expand for a HUD that scales with the window and stays anchored to the screen edges.
  • Anchor each HUD group to the corner or edge it belongs to, with the layout presets in the toolbar.
  • Build the HUD at the base window size set in Project Settings, and choose the 1x or the double-size PNGs to suit that size, so the art is not enlarged much on screen.
  • Pieces with detail in the middle of an edge, such as a frame with tick marks, should be used at their own size and not nine-sliced.

If it looks wrong

  • Corners look squashed. The node is smaller than the two margins added together. Give it a larger minimum size.
  • The border is blurred or doubled. A margin is too small and part of the corner is being stretched. Raise it by a few pixels.
  • The margins are right but everything is half size. The values are for the 1x image and the texture is the 2x one.

Lowlight HUD Kit: 258 Sci-Fi Game UI Pieces lists the margins for 47 stretchable pieces in each theme, in the order Godot's fields use.

Questions

What is nine-slice scaling?

The image is cut into nine pieces by two vertical and two horizontal lines. The four corners keep their size, the four edges stretch along their length and the centre stretches both ways, so a panel can be any size without its border changing thickness.

Which Godot 4 node does nine-slice?

NinePatchRect for panels and frames. For buttons use a StyleBoxTexture with texture margins, and for bars a TextureProgressBar with Nine Patch Stretch switched on.

How do I find the patch margins of an image?

Use the list that comes with the art if there is one. Otherwise open the image, measure from each side to just past the corner art, and use those four distances in pixels.

All guides

Back to the home page