Game UI

Nine-slice

A nine-slice cuts a picture with two upright and two level lines. Resized, the four corners keep their size, the four sides stretch along themselves only, and the middle fills what is left. One small texture then serves a dialog, a tooltip and a full-screen panel, and its border is never smeared.

A 96 × 96 panel texture with its four cuts, 24 in from each edge
A 96 × 96 panel texture with its four cuts, 24 in from each edge

Where to put the cuts#

Put each cut just inside the part that must not stretch: past the corner ornament, past the rounded corner, past the end cap of a bar. For the panel above, each corner block with its jewel is 24 pixels square, so the cuts are at 24.

  • A button with round ends 16 wide: cut 16 from the left and right, 12 from the top and bottom.
  • A bar that only grows wider: cut the left and right, and leave the top and bottom at 0. That gives three parts in place of nine.
  • Keep the stretching middle plain. A flat color or a soft gradient stretches well. A pattern does not: tile it.
The same panel at three sizes, and stretched whole with no cuts
The same panel at three sizes, and stretched whole with no cuts

Set the cuts#

By hand
  1. Place the texture with File › Place Image… (⇧⌘K) and select it.
  2. In the inspector, press the plus beside Game UI.
  3. Type the four numbers: Slice L, Slice R, Slice T, Slice B. They are distances in from each edge, at the size the picture has when you first cut it.
  4. Or drag on the canvas. With the section open, move the pointer just inside an edge of the picture, 4 to 12 pixels in. The pointer turns into a resize arrow. Drag inward and a cut line follows. The numbers in the inspector follow as you go.
  5. Resize the picture by its handles. The corners hold.

Once a picture has a cut, its cut lines show whenever it is selected, and you can drag any of them at any time. A cut stops one unit short of the cut facing it, and never reaches the edge. Each drag is one step to undo.

With an agent

Give the agent the texture's size and where its border ends. The property is slice: left, right, top, bottom, then the width and height the cuts are measured at.

Place panel.png (96 × 96) as a dialog panel, 360 × 180. Its corner ornaments
are 24 wide, so cut it 24 in from every edge.

The agent calls create_nodes with:

{
  "nodes": [
    {
      "kind": "rectangle",
      "name": "Dialog panel",
      "x": 780,
      "y": 450,
      "width": 360,
      "height": 180,
      "fills": [{ "image": "/Users/you/game/ui/panel.png", "fit": "stretch" }],
      "slice": [24, 24, 24, 24, 96, 96]
    }
  ]
}

Stretch or tile#

Under the four numbers, choose Stretch or Tile.

  • Stretch draws each side and the middle out to fit. Use it for flat or softly shaded art.
  • Tile lays each side and the middle side by side at their own size, and cuts the last copy where the room ends. Use it for studs, rivets, stitches, hatching, or pixel art that must stay at its own scale.
Stretch pulls the studs and the hatching out. Tile repeats them as drawn.
Stretch pulls the studs and the hatching out. Tile repeats them as drawn.

Through the tools, Tile is a seventh number, 1, at the end of slice. This call to update_nodes switches a panel to Tile:

{
  "changes": [
    { "id": "4:12", "slice": [24, 24, 24, 24, 96, 96, 1] }
  ]
}

Draw a tiling texture so that its side and its middle repeat cleanly: a stud every 12 pixels on a side 48 long, a hatch that repeats every 8.

On frames and on shapes#

The Slice fields are there for three kinds of thing, and they mean something a little different for each.

OnWhat the cuts do
A pictureThe picture is drawn in nine parts, stretched or tiled
A frame or componentWhat is in the frame keeps to its part of the frame when the frame is resized. A child in a corner is moved and never stretched. A child along a side stretches along that side only
A shape (rectangle, ellipse, path, combined shape)The shape is drawn as before. Its cuts go into the slice file when you export it, so the engine stretches the exported picture correctly

A frame cut in nine is how you make a panel from vector parts instead of a texture: a border, four corner ornaments and a title bar, all editable, that resize as one.

A frame cut 36 in from each edge: resized, its corner jewels move and its title bar stretches
A frame cut 36 in from each edge: resized, its corner jewels move and its title bar stretches

While a frame is cut, its cuts decide where its children go, in place of the children's pins. A frame with auto layout places its children by its layout. There is no Tile for a frame: what is in a frame is never repeated.

Note

Cuts on a vector button matter at export. Draw a 200 × 64 button with an 18 radius, set its cuts to 24, and the slice file tells the engine which 24 pixels at each end hold the round corners. See Export for a game engine.

Check your work#

  • Resize the panel to its smallest and largest size in the game. Corners that deform mean a cut sits inside an ornament.
  • Keep a cut picture larger than its two cuts added together: more than 48 across for cuts of 24 and 24. Below that there is no middle left to give.
  • For pixel art, keep the panel at whole multiples of the texture's scale so every pixel of the border lands on one pixel of the screen.
  • To set slice to nothing, give null, or type 0 in all four fields.