Version: Unity 6.6 Alpha (6000.6)
Language : English
9-slicing
Masking sprites

9-slice a sprite

Divide a spriteA 2D graphic objects. If you are used to working in 3D, Sprites are essentially just standard textures but there are special techniques for combining and managing sprite textures for efficiency and convenience during development. More info
See in Glossary
into nine sections so Unity can stretch or repeat the different sections, and add a collisionA collision occurs when the physics engine detects that the colliders of two GameObjects make contact or overlap, when at least one has a Rigidbody component and is in motion. More info
See in Glossary
shape.

Create a sprite with 9 sections

Follow these steps:

  1. Select the sprite texture in the Project window.

  2. In the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
    See in Glossary
    window, set MeshThe main graphics primitive of Unity. Meshes make up a large part of your 3D worlds. Unity supports triangulated or Quadrangulated polygon meshes. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. More info
    See in Glossary
    Type
    to Full Rect, then select Apply.

    If you set Mesh Type to Tight instead, 9-slicing might not work correctly, because of the way Unity generates and renders the sprite.

  3. Select Open Sprite Editor.

  4. Select the sprite you want to 9-slice.

  5. Click and drag the green handles inward to define the borders of the sprite, for example the walls of a floor tile. Or enter the values in the Sprite overlay, using the L, R, T, and B fields for left, right, top, and bottom.

    A floor texture with 4 surrounding walls.
    A floor texture with 4 surrounding walls.

    The borders define the nine areas of the sprite: the central area (E), the four walls (B, D, F, and H), and the four corners (A, C, G, and I).

  6. Select Apply.

  7. Drag the sprite asset from the Project window to the SceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
    See in Glossary
    view as normal.

  8. In the Inspector window for the sprite, in the Sprite RendererA component that lets you display images as Sprites for use in both 2D and 3D scenes. More info
    See in Glossary
    component, set Draw Mode to Sliced or Tiled depending on the behavior you want. For more information, refer to 9-slicing modes.

Note: Setting Draw Mode to Sliced or Tiled means the Sprite Renderer component controls the ColliderAn invisible shape that is used to handle physical collisions for an object. A collider doesn’t need to be exactly the same shape as the object’s mesh - a rough approximation is often more efficient and indistinguishable in gameplay. More info
See in Glossary
2D component. As a result, you can’t edit the Collider 2D component manually.

When you resize this sprite, the sections behave as follows:

  • The four corners (A, C, G and I) do not change in size.
  • The top and bottom walls (B and H) horizontally stretch or repeat, depending on Draw Mode.
  • The left and right walls (D and F) vertically stretch or repeat, depending on Draw Mode.
  • The central floor (E) stretches or repeats, depending on Draw Mode.

If you set Draw Mode to Sliced, Unity uses simple scaling if you resize the sprite using the Transform component of the GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary
or the transform tool in the Scene viewAn interactive view into the world you are creating. You use the Scene View to select and position scenery, characters, cameras, lights, and all other types of Game Object. More info
See in Glossary
.

Configure tiling

If you set Draw Mode to Tiled, use the Tile Mode property to control how Unity repeats, or tiles, the texture. The options are:

  • Continuous: The texture doesn’t stretch. The tiles at the edges might use cropped parts of the texture.
  • Adaptive: The center of the texture stretches until the width or height reaches the Stretch Value, at which point it repeats. Each tile always uses the full texture.

A value of 1 for Stretch Value means the sprite repeats when the sprite is twice its original size. A lower value means the sprite repeats less often.

The floor texture with Tile Mode set to Continuous.
The floor texture with Tile Mode set to Continuous.
The floor texture with Tile Mode set to Adaptive, and Stretch Value set to 1.
The floor texture with Tile Mode set to Adaptive, and Stretch Value set to 1.
The floor texture with Tile Mode set to Adaptive, and Stretch Value set to 0.5.
The floor texture with Tile Mode set to Adaptive, and Stretch Value set to 0.5.

Add a collision shape

To add a collision shape to a 9-sliced sprite, follow these steps:

  1. Select the sprite in the Hierarchy window.

  2. In the Inspector window, select Add Component.

  3. Select either a Box Collider 2D or Polygon Collider 2D component.

    Other types of Collider 2D component don’t support 9-slicing.

  4. Enable Auto Tiling. Unity now automatically updates the collider shape when the dimensions of the sprite change.

Unity can add additional edges to the collider shape when you enable Auto Tiling. This can have an effect on collisions.

Additional resources

9-slicing
Masking sprites