Gridwright Start for $1

9 Slice in Unity: Sprite Borders That Don't Stretch

9 slice in Unity step by step: set sprite borders in the Sprite Editor, use Sliced and Tiled on UI Images and Sprite Renderers, and keep pixel art sharp.

9-Slice Previewer at work
9-Slice Previewer at work: Panels and buttons that stretch to any size, checked before your game shows them.

You draw one nice panel for your menu, drop it on a Unity Image, drag it wider, and the corners turn into ovals. The border goes fat on one side and thin on the other, the pixel outline smears, and a button that looked crisp at 48 pixels looks melted at 200. So you draw the panel again at every size you need, and then the dialogue box has to grow with its text.

9-slice scaling fixes that. You mark four borders on the picture once, and Unity keeps the four corners exactly as drawn, stretches or tiles the four edges in one direction only, and fills the middle. One panel then works at every size. This guide sets it up step by step, for UI Images and for Sprite Renderers, with the settings that keep pixel art sharp. Then the 9-Slice Previewer finds the right borders for you in seconds.

How 9-slice works

The four borders cut the picture into nine parts:

Pixel UI Kit
Pixel UI Kit: 16x16 pixel art UI and HUD kit for RPG and platformer games: panels, buttons, bars, icons, 6 themes, 9-slice ready. One of the 150 packs in Gridwright.
  • 4 corners: never scaled. Rounded corners, rivets and shadows stay exactly as drawn.
  • Top and bottom edges: stretched or tiled across only.
  • Left and right edges: stretched or tiled up and down only.
  • The middle: fills whatever is left, in both directions.

The borders are measured in pixels of the source image, from each side inwards. A 48x48 panel with a 6 pixel frame and a 2 pixel shadow gets borders of 8 on the sides that carry the shadow and 6 on the others. Each border has to reach past everything that must not stretch: the outline, the bevel, the shadow and the corner ornaments.

Step 1: import the sprite

Select the PNG in the Project window and set, in the Inspector:

Fantasy RPG UI
Fantasy RPG UI: 32x32 pixel art RPG UI: ornate panels, slots, orbs, bars, cooldowns, cursors, icons, 4 themes. One of the 150 packs in Gridwright.
  • Texture Type: Sprite (2D and UI).
  • Sprite Mode: Single for one panel, Multiple for a UI sheet with many pieces.
  • Mesh Type: Full Rect. The default, Tight, follows the shape of the pixels, and a Sprite Renderer can't slice or tile that mesh. UI Images don't care, but Full Rect costs nothing for a rectangle anyway.
  • Filter Mode: Point (no filter) for pixel art, so stretched edges don't blur and the slices don't show thin seams.
  • Compression: None, so the outline colours stay exact.

Press Apply.

Step 2: set the borders in the Sprite Editor

Press Sprite Editor in the Inspector. If Unity says the Sprite Editor is missing, install the 2D Sprite package in the Package Manager; it ships with the 2D templates.

Click the sprite (for a sheet, slice it first and click each piece). The Sprite panel in the bottom right shows the Border fields: L, T, R and B. Type the four numbers, or drag the green lines that sit on each edge of the sprite until they lie just inside the frame. Press Apply at the top.

Two things to check before you leave:

  • Every corner must sit fully inside its border lines. A rivet that crosses a green line will be stretched.
  • The borders of opposite sides must not overlap. If L plus R is wider than the sprite, there is no middle left to stretch.

In a script the same values come back as sprite.border, a Vector4 in the order left, bottom, right, top, which is handy when you build panels from code.

Step 3a: a UI panel with Image

For anything on a Canvas (menus, dialogue boxes, buttons, health bar frames):

  1. Add an Image and drag your sprite into Source Image.
  2. Set Image Type to Sliced.
  3. Resize the RectTransform. The corners stay, the edges and middle stretch.

If the Inspector shows "This Image doesn't have a border", Step 2 wasn't applied, or the Image uses another sprite of the sheet.

Useful options next to it:

  • Fill Center: turn it off for a frame with an empty middle, such as a selection box around an item.
  • Pixels Per Unit Multiplier: draws the borders bigger or smaller. For pixel art keep it on a whole value, or the frame lines change width.
  • Image Type Tiled: repeats the edges and the middle instead of stretching them. Use it when the edge has a pattern (a rope, studs, a stone course) that would smear when stretched.

Step 3b: a world object with Sprite Renderer

For things in the scene rather than on the UI (a sign, a platform, a dialogue bubble over a character):

  1. On the Sprite Renderer, set Draw Mode to Sliced or Tiled.
  2. Change the Size field (or drag the blue handles of the Rect tool), not the Transform's Scale. Scale stretches everything, corners too.
  3. With Tiled, Tile Mode decides what happens at the end of an edge: Continuous repeats the tile and cuts the last one, Adaptive stretches it until a full extra tile fits (set by Stretch Value).

If Unity warns that the sprite isn't generated with Full Rect, go back to Step 1 and change Mesh Type.

The same borders also work with UI Toolkit: there you set -unity-slice-left, -unity-slice-right, -unity-slice-top and -unity-slice-bottom on the element, in pixels of the source image.

Keep pixel art panels sharp

9-slice keeps the corners, but pixel art also needs whole pixels everywhere else:

  • Sizes in whole pixels of the art. At 3x, a panel should be a multiple of 3 screen pixels wide and high, or one row of the frame gets doubled.
  • Canvas scale. A Canvas Scaler set to Scale With Screen Size can land on 2.37x and blur every line. Pick reference resolutions that divide your target screens, and tick Pixel Perfect on a Screen Space Canvas.
  • Tiled edges with patterns. Make the stretchable part of the edge exactly one pattern repeat long, and size panels in whole repeats, so the last tile isn't cut in half.
  • Thin middles. If the middle is a flat colour, a 1 or 2 pixel wide middle is enough and keeps the texture small.

Common problems and their fix

What you seeWhyFix
Corners stretchImage Type is Simple, or no bordersSliced, and Apply in the Sprite Editor
Thin lines between slicesBilinear filter or compressionFilter Mode Point, Compression None
Sprite Renderer won't sliceMesh Type TightMesh Type Full Rect
Frame lines change widthNon-whole scale or multiplierWhole scales and whole sizes
Pattern on the edge smearsEdges stretchImage Type Tiled
Panel too small to shrinkBorders too wideSmaller borders: only the frame needs to stay

Find the borders in seconds

The slow part is the numbers: try 6, apply, resize, see the shadow stretch, try 8. The 9-Slice Previewer does that part in your browser:

  • Drop a panel, a button or a whole UI sheet.
  • Drag the four guides, or press Find margins: it picks the smallest margins that still stretch cleanly.
  • Drag the panel's corner and see it at any size, stretched, tiled or tiled to fit, before you touch Unity.
  • Take the margins for Unity, GameMaker, Phaser, Ren'Py, Defold and Unreal, or the piece cut from its sheet, a JSON of every piece's margins, CSS border-image classes and a Godot 4 NinePatchRect and StyleBoxTexture.

It runs on your computer and never uploads your art. Then type the four numbers into Unity's Border fields and press Apply.

Your game, looking finished

Every Gridwright asset pack, made to fit together, and all 44 tools for the fiddly work, with every new release, for $17 a month. Try it in your own game first: 3 days for $1 with every tool and up to 10 packs. Cancel any time.

Start: 3 days for $1 See everything it includes

Quick checklist

  • Texture Type Sprite (2D and UI), Mesh Type Full Rect, Filter Mode Point, Compression None.
  • Borders set and applied in the Sprite Editor, every corner inside its lines.
  • Image Type Sliced on the UI, Draw Mode Sliced on Sprite Renderers, resized by Size, not Scale.
  • Tiled for patterned edges, whole sizes and whole scales for pixel art.

Do it once per panel and the same sprite serves every menu, tooltip and dialogue box in your game. You can open the 9-Slice Previewer here.