9-Slicing for Game UI in Unreal and Unity
How 9-slicing keeps UI corners sharp at any size, how to set it up in Unreal UMG (Box margins) and Unity (sprite borders), and how to draw art for it.
In this article
Every game has buttons, panels, tooltips and dialog boxes in many sizes. Drawing a separate image for each would be slow and waste memory, and simply stretching one image ruins its rounded corners and borders. 9-slicing (also called 9-patch) solves this with one small texture.
How it works
Four borders, left, right, top and bottom, cut the image into a 3 × 3 grid:
| Left column | Middle column | Right column | |
|---|---|---|---|
| Top row | Corner: fixed | Edge: stretches horizontally | Corner: fixed |
| Middle row | Edge: stretches vertically | Centre: stretches both ways | Edge: stretches vertically |
| Bottom row | Corner: fixed | Edge: stretches horizontally | Corner: fixed |
Draw the image at 400 × 80 for a wide button or 200 × 300 for a panel, and the corners look exactly the same; only the parts between them grow.
Unreal Engine: UMG Box brushes
In UMG, 9-slicing is a property of the brush on an Image, Button or Border widget:
- Select the widget and open its Brush (for buttons, the Normal, Hovered and Pressed styles each have one).
- Set Draw As to Box.
- Set Margin (left, top, right, bottom).
The margins are fractions of the texture size, from 0 to 1, not pixels:
margin left = border left px ÷ texture width margin top = border top px ÷ texture height
So a 16 px border on a 64 × 64 texture is 0.25. Draw As: Border uses the same margins but draws only the edges, leaving the centre empty, which is handy for frames.
Keep the brush's Image Size at the texture's real size, so the corners appear at their pixel size; DPI scaling then scales them with the rest of the UI. If you want consistent UI across screen sizes, UI Scaling in Unreal and Unity covers DPI curves and the Canvas Scaler.
Unity: sprite borders
In Unity the borders belong to the sprite, in pixels:
- Select the texture, set Texture Type to Sprite (2D and UI) and apply.
- Open the Sprite Editor and drag the green border handles, or type L, T, R, B in pixels. Apply.
- On the UI Image, set Image Type to Sliced.
Options worth knowing:
- Fill Center off draws only the frame.
- Image Type: Tiled repeats the edges and centre instead of stretching them, useful for patterned borders.
- Pixels Per Unit Multiplier on the Image makes corners bigger or smaller without changing the texture.
- For a Sprite Renderer (not UI), use Draw Mode: Sliced and set the sprite's Mesh Type to Full Rect.
In scripts, Sprite.border is a Vector4 in the order left, bottom, right, top, not the L, T, R, B order of the Sprite Editor.
Drawing art that slices well
- Put detail in the corners. Ornaments, rivets and highlights belong in the corners; the edges and centre get stretched.
- Keep edges and centre plain or evenly patterned. A gradient across the centre stretches with it; a vertical gradient across a horizontally stretched edge is fine.
- Make borders cover the whole rounded corner, plus a pixel or two. If the curve crosses into an edge, it gets stretched into an oval.
- Stay small. The texture only needs to hold the corners and a little edge; 32–128 px is typical.
- Pixel art: use point (nearest) filtering and scale by whole numbers, or the pixels become uneven.
- Never draw smaller than the borders. If a widget is narrower than left + right borders, the corners have to shrink and overlap.
Preview before you import
Finding the right borders by trial and error in the engine is slow. The free 9-Slice Previewer lets you drop in a button or panel image, drag the slice lines, and see it stretched as a wide button, a tall panel and a small square at once, with stretch or tile and a corner scale. It gives you both sets of numbers to paste: the UMG Box Margin for Unreal and the L, T, R, B pixels for Unity.
Summary
- 9-slicing keeps corners fixed and stretches edges and the centre, so one small texture makes UI of any size.
- Unreal UMG: Draw As Box, Margin as fractions of the texture (pixels ÷ size).
- Unity: borders in pixels in the Sprite Editor, Image Type Sliced or Tiled.
- Put detail in the corners, keep edges simple, and make borders cover the rounded corners.
Frequently asked questions
What is 9-slicing in game UI?
A technique for scaling UI images without distorting them. Four borders split the image into nine parts: corners stay the same size, edges stretch in one direction and the centre stretches in both.
What do the UMG Box margin values mean?
They are fractions of the texture size from 0 to 1. Left and right are divided by the texture width, top and bottom by its height. A 16 px border on a 64 px texture is 0.25.
Why is my 9-sliced sprite not working in Unity?
Check that the texture type is Sprite (2D and UI), that borders are set in the Sprite Editor and applied, and that the Image's Image Type is Sliced. For a Sprite Renderer in Sliced draw mode, also set the sprite's Mesh Type to Full Rect.
Should 9-slice textures be large?
No. The size on screen comes from the widget, not the texture. A 32 to 128 px panel with clean corners is usually enough, and smaller textures save memory.