Skip to content

How to Add an Outline to a Sprite in Unity

An outline on a sprite is drawn by its shader. For every transparent pixel, the shader looks at the pixels a set distance away, and if one of them is solid, it paints this one in the outline colour. So you need two things: a shader that does this, which Unity’s default sprite shader does not, and empty space around the sprite for the outline to be painted into. Most outlines that come out cut off are short of that space.

The demo's space soldier with a gold outline that grows in around it, glows under Bloom, then fades away again

The outline can only appear where the sprite has a mesh to draw on, and only on pixels that are transparent in the texture. Three things take that room away.

The sprite’s Mesh Type is Tight. Tight is Unity’s default. It builds a mesh that hugs the visible pixels, to save drawing empty space, and that empty space is exactly where the outline goes. In the sprite’s import settings, set Mesh Type to Full Rect and press Apply. Unity always uses Full Rect for sprites smaller than 32 by 32 pixels, which is why small sprites often outline fine while big ones get clipped.

There is no transparent room in the texture. With Full Rect the mesh covers the sprite’s whole rectangle, and no more. If the art touches the edge of its image, or of its slice in a spritesheet, the outline stops flat along that edge. Give the image a transparent border at least as wide as the outline, and when you slice a spritesheet, make each slice bigger than its art.

Another sprite is within reach. The outline reads the pixels around each pixel, and nothing stops it at the edge of a slice. In a spritesheet or a Sprite Atlas, a neighbour closer than the outline’s width shows up as stray bits of outline along the edge. Keep the gap between one sprite’s art and the next wider than the outline: spacing between slices, or transparent borders plus the Sprite Atlas Padding.

The soldier with the same thick gold outline three times, its mesh drawn over it in black. With Mesh Type Tight the outline is cut off along the mesh that hugs the soldier. Cropped to its visible pixels, the outline stops flat at the top, the sides and the feet. With Full Rect and a transparent border, the outline goes all the way around

Too thin rather than cut off? The width is counted in the texture’s own pixels. A sprite whose image is 2048 pixels tall, shown 200 pixels tall, needs ten times the width of one whose image is 200 pixels. If the sprite is never shown bigger, lower Max Size in its import settings to something near the size it is shown at; otherwise raise the width.

Textures Setup covers these import settings for every effect, not only the outline.

You can do it without buying anything. Three ways, simplest first.

Draw the outline into the art. Free, it costs nothing at runtime, and for pixel art it is often the right call. It cannot change colour, grow, glow or switch off in the game, and every sprite needs it drawn by hand.

Copies behind the sprite. Four or eight copies of the sprite, each nudged by the outline’s width in a different direction and sorted behind it. A Sprite Renderer’s Color multiplies the art, so black copies work out of the box, but any other colour needs a material that paints the sprite one flat colour. It costs four to eight extra sprites per outline, and at larger widths the copies come apart around thin parts. A single scaled up copy only works for round shapes. For UI, Unity ships this method as its Outline component, see Outlines on UI Images.

A small shader. This is the core of it, in a sprite shader with ordinary alpha blending (Blend SrcAlpha OneMinusSrcAlpha), where uv is the sprite’s texture coordinate, i.color its vertex colour, and _OutlineWidth is in texture pixels:

// _MainTex_TexelSize.xy is one pixel of the texture, filled in by Unity
float2 offset = _OutlineWidth * _MainTex_TexelSize.xy;
float around = tex2D(_MainTex, uv + float2( offset.x, 0)).a
+ tex2D(_MainTex, uv + float2(-offset.x, 0)).a
+ tex2D(_MainTex, uv + float2(0, offset.y)).a
+ tex2D(_MainTex, uv + float2(0, -offset.y)).a;
half4 sprite = tex2D(_MainTex, uv);
// 1 where a neighbour is solid and this pixel is not
float outline = step(0.05, around) * (1 - sprite.a);
return lerp(sprite * i.color, _OutlineColor, outline);

Declare float4 _MainTex_TexelSize; next to _MainTex and Unity fills it in. In Shader Graph, in a Sprite Unlit graph, it is the same idea in nodes: four Sample Texture 2D nodes whose UVs come from Tiling And Offset nodes, offset by the width divided by the Texture Size node’s Width and Height; their alphas added, then a Step, times One Minus the sprite’s own alpha; and that result as the T of a Lerp from the sprite’s colour to the outline colour, alpha included. Effects in your Shader Graph shows that graph, and the same outline as a Custom Function.

What you will run into:

  • The width is limited by the room around the sprite, so everything in the section above still applies.
  • With four directions, corners come out notched as the outline gets wider. Sampling the four diagonals as well fills them, for four more texture reads per pixel.
  • Past a few pixels the outline is really copies of the silhouette, and they come apart around thin parts.
  • Glow, animation, pixel perfect steps and a clean way to switch it on and off are yours to add.

Don’t own it yet? → Get it on the Unity Asset Store

All In 1 Sprite Shader has this outline built in. Add the AllIn1Shader component to the sprite or UI Image (Add Component → AllIn1SpriteShader → AddAllIn1Shader), which gives it its own material, then tick Outline in the material’s Color Effects. First Steps walks through the component. It works in the Built-in Render Pipeline and in URP straight after import, see Render Pipeline And Post Processing Setup.

The Outline effect open in the material: Outline Base Color, Outline Base Alpha, Outline Base Glow, Outline High Resolution, Outline is Pixel Perfect, Outline Base Width, Outline uses texture, Outline uses distortion and Only render outline
  • Outline Base Color, Outline Base Alpha and Outline Base Glow set its colour, its opacity and how bright it is. Glow multiplies the colour, so above 1 it only reads as a glow with Bloom in the scene; Textures Setup links the setup videos.
  • Outline High Resolution? reads eight directions instead of four, which fills the corners.
  • Outline Base Width sets how thick it is, in the texture’s pixels at 0.005 per pixel: the default, 0.004, is under one pixel, and the maximum, 0.2, is 40.
  • Outline is Pixel Perfect? swaps the width for Outline Base Pixel Width, a whole number of the texture’s pixels. It is made for pixel art, with the texture’s Filter Mode on Point.
  • Outline uses texture? paints a scrolling texture onto the outline, tinted by its colour.
  • Outline uses distortion? wobbles the outline with a scrolling noise texture.
  • Only render outline? draws the outline alone, without the sprite.

Thick outlines on pointed shapes. The outline is the sprite’s shape copied a width away in four directions, or eight with High Resolution. Past a few pixels those copies stop touching at thin points such as spikes, and each point splits into several tips. On spiky art, keep the outline to a few pixels and High Resolution on.

A pixel art version of the soldier with its outline growing to four pixels and back. With Outline Base Width the outline breaks into half pixels and stray specks as it grows; with Outline is Pixel Perfect it grows one whole pixel at a time

The Mesh Type and the transparent border matter here just as much. When a single sprite has a border, but not enough of one, the Rect Size effect enlarges its mesh in the shader to make more room. It does not work on sprites sliced from a spritesheet, it needs the texture’s Wrap Mode on Clamp, and it is a last resort on UI Images. It cannot rescue art that touches the edge of its image, because Clamp stretches those edge pixels across the new room. And it grows the mesh from the sprite’s pivot but the texture from its centre, so keep the pivot in the centre or the sprite drifts away from it.

Two more outlines draw inside the sprite, so they need no room at all:

  • Alpha Outline recolours the sprite’s own semi transparent edge pixels. It is cheaper, but it needs soft edges, so it does nothing on crisp pixel art.
  • Inner Outline draws lines on top of the sprite wherever neighbouring colours differ, so it outlines the shapes inside the art.

Every setting is listed with its property name on Effects and Properties Breakdown.

One outline around a character made of several sprites

Section titled “One outline around a character made of several sprites”

When a character is several sprites, such as a head, a body and an arm holding a weapon, an outline on each part draws a line wherever the parts overlap, cutting across the character. To get one outline around the whole silhouette, give every part a second copy that carries the outline, sorted behind all the parts.

The demo's three overlapping soldiers bobbing up and down. With the outline effect on each sprite, every soldier's outline cuts across the ones behind it; with the Unified Outline there is one outline around the whole group

The asset’s All1CreateUnifiedOutline component builds those copies:

  1. Save a material with the outline you want. Ticking Only render outline? keeps the copies from drawing each part a second time.
  2. Add the component to the character’s root object and put that material in Outline Material.
  3. Tick Create Unified Outline. Every Sprite Renderer under the root, the root’s own included, gets an outline copy on Order in Layer -100 of the Default sorting layer, unless you change those two fields, and the component removes itself.

Each copy is a child of its part, so it follows the part as it moves, turns and scales. If you fill in Outline Parent Transform, the copies go there instead and no longer follow the parts. Each copy also takes its part’s sprite and flip once, when it is made, so an animation that swaps sprites does not update it. More on Unified Outline.

The Outline effect works on a UI Image the same way: add the AllIn1Shader component to the Image and tick Outline. What changes:

  • An Image draws its sprite on a plain rectangle unless Use Sprite Mesh is ticked, so Mesh Type does not cut the outline off. The texture still needs its transparent border.
  • The width is still counted in the texture’s pixels, so a large image shown as a small icon needs a larger width.
  • Inside a Mask or a Scroll View, the default shader variant is not masked. Switch to the UI Mask variant, see UI Masking.
  • Images that share a material share its outline. To change one from code, give it its own copy, see Scripting.

Unity’s own option is the Outline component (Add Component → UI → Effects → Outline). It draws four copies of the image, offset diagonally by Effect Distance, each one the image multiplied by Effect Color. That gives a solid outline in black, or in any colour on a white image such as an icon or text, and past a few pixels the four copies separate. It does not glow.

Hover and selection highlights are the usual reason. Leave Outline on in the material and move Outline Base Alpha (_OutlineAlpha) between 0 and 1. That changes a number and nothing else, so the outline behaves the same in a build as in the editor. Switching the effect itself with EnableKeyword("OUTBASE_ON") is the route that breaks in builds, see the next section.

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class OutlineOnHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
private static readonly int OutlineAlpha = Shader.PropertyToID("_OutlineAlpha");
private Material mat;
private void Awake()
{
if (TryGetComponent(out Image image))
{
// UI Images share their material, so each one gets its own copy
image.material = new Material(image.material);
mat = image.material;
}
else
{
mat = GetComponent<Renderer>().material;
}
mat.SetFloat(OutlineAlpha, 0f);
}
public void OnPointerEnter(PointerEventData eventData) => mat.SetFloat(OutlineAlpha, 1f);
public void OnPointerExit(PointerEventData eventData) => mat.SetFloat(OutlineAlpha, 0f);
}

The pointer events need an EventSystem in the scene, which Unity adds when you create a Canvas from the GameObject menu. On a sprite they also need a Collider 2D on the sprite and a Physics 2D Raycaster on the camera. Inside a Mask, write to image.materialForRendering instead, as Scripting explains. Both ways of switching effects are compared on How to Enable/Disable Effects at Runtime.

When the outline shows in the editor but not in the build, the usual cause is that the build has no compiled shader variant for that material. Unity builds the variants your project’s materials use, while the editor compiles any variant on demand, so the editor never shows the problem. It happens when Outline (OUTBASE_ON) or one of its options is switched on from code with EnableKeyword, or on a material created in code.

Turn Outline on in a material asset in the editor and drive it with Outline Base Alpha, as above. If you must switch keywords, keep a material in the project for each combination you switch to, as Making sure the variants exist explains. The FAQ covers the other things that differ between the editor and a device.

  • Soft or feathered outlines. The outline has a hard edge: a pixel is either in it or not. The nearest thing is Outline Base Glow with Bloom, which adds a soft halo around it.
  • A fixed width on screen. The width is counted in the texture’s pixels, so the outline grows and shrinks with the sprite and with the camera’s zoom.