Skip to content

Palette Swap and Color Swap for Sprites in Unity

There are three ways to give a sprite new colours, and they suit different jobs. Tinting multiplies the whole sprite by one colour. Hue shifting turns every colour around the colour wheel by the same angle. Swapping replaces chosen colours, or chosen regions, with others and leaves the rest alone: the red team’s cloak turns blue while the skin and the sword stay as they were. A true palette swap, the way old consoles did it, stores an index in each pixel and looks its colour up in a palette, so any number of colours change at once.

The same soldier four times in red, blue, green and gold team colours, made with Color Swap on the helmet and legs, the visor and the spikes, each keeping the sprite's shading
  • Tint (the SpriteRenderer’s Color) needs nothing extra and darkens everything with it. Good for a sprite drawn in greys meant to be coloured; poor for a sprite already in colour, since red tinted blue goes nearly black.
  • Hue shift keeps the shading and the contrast and moves every colour together. Good for “the same enemy, another variant”; poor when only part of the sprite should change, since the skin shifts with the armour.
  • Swap changes only what you choose. It is the one for team colours, skins and unlockable outfits.
The soldier as it is, then aimed at red three ways. Tinted red through the SpriteRenderer's Color it goes dark and the blue turns nearly black. With Hue Shift every colour turns, the helmet olive and the red spikes blue. With Color Swap only the helmet and legs turn red

For pixel art with a fixed palette, the classic approach is two textures:

  1. An index texture: the sprite drawn in greys, where each grey is a slot in the palette. With 16 colours, slot 0 is black, slot 15 is white, and each slot between is one step of 1/15.
  2. A palette texture: a strip 16 pixels wide and 1 high, one colour per slot. Each skin or team is another strip.

In Shader Graph, a Sprite Unlit graph: sample the index texture, take its R into the X of a Vector 2 with Y at 0.5, and use that as the UV of a second Sample Texture 2D that reads the palette. The palette goes into Base Color, the index texture’s alpha into Alpha. URP’s sprite targets multiply the result by the renderer’s Color on their own, so the graph needs no Vertex Color node.

The Sprite Unlit graph: _MainTex, the index texture, into Sample Texture 2D; its R into the X of a Vector 2 whose Y is 0.5; that Vector 2 as the UV of a second Sample Texture 2D reading Palette; its RGBA into Base Color and the index texture's A into Alpha

Three import settings make or break it, on both textures:

  • Filter Mode: Point. Bilinear filtering blends neighbouring slots and gives colours that are in no palette.
  • Compression: None. Compression shifts the greys, so pixels land in the wrong slot.
  • sRGB off on the index texture. It holds data, not colours; with sRGB on, a Linear project darkens it and every slot moves down.
  • Wrap Mode: Clamp on the palette. The last slot’s R of exactly 1 reads the strip at its right edge, and Repeat wraps that round to the first colour.
The soldier stored as 16 greys and drawn by the graph through three 16 by 1 palettes, each strip shown under its soldier: its own colours, the same colours with their hues turned, and a two tone ramp from dark blue to mint

Swapping palettes is then setting a different strip on the material, or on a MaterialPropertyBlock per character. Lighting it under 2D lights means a Sprite Lit graph instead; the swapped colour goes into Base Color like any other.

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

Add the AllIn1Shader component to the sprite (Add Component → AllIn1SpriteShader → AddAllIn1Shader). Four effects in the material’s Color Effects recolour it, each in its own way (Effects and Properties Breakdown):

Color Swap: three regions from a mask. Give it a Color Swap Texture painted in pure red, green and blue over the parts to recolour, black elsewhere. Each region takes its new colour (Red Channel, Green Channel, Blue Channel) and keeps the sprite’s own shading, since the new colour is multiplied by each pixel’s brightness. Unpainted parts keep their colours. Color Swap Blend fades the whole effect in and out.

The soldier, its Color Swap Texture painted red on the spikes, ear and gun, green on the visor and blue on the helmet and legs, and the result with a blue helmet and legs, a pink visor and gold spikes
  • The mask has to match the sprite pixel for pixel: the same size and layout as the sprite’s texture. For a sprite sheet, that means a mask sheet with every frame painted, which is why one customer swapped mask textures per character with the Animator.
  • Lines along the edges of a region come from a mask that is filtered or compressed: set the mask to Point filtering and no compression, like the sprite.
  • A region that looks faint or too dark is dark in the sprite: the new colour is multiplied by its brightness. Raise that channel’s Luminosity to lift it.
The helmet and legs swapped to the same blue twice. With Blue luminosity at 0 the dark legs and the helmet's shade come out dark. At 0.6 the same parts are lifted

Change 1 Color: up to three exact colours. Pick a Color to change and a New Color, and every pixel close enough to it changes, its shading kept. Tolerance decides how close: higher changes only the nearest matches, lower reaches further. Use Color 2 and Use Color 3 add two more slots, for three colours in all, with no mask to paint. It suits pixel art with flat colours better than painted art with gradients.

The soldier as it is and with Change 1 Color's three slots aimed at its helmet blue, spike red and visor orange: the helmet turns green, the spikes and gun brown, the visor pink

Hue Shift: everything at once. Hue Shift turns the colours by 0 to 360 degrees, with Saturation and Brightness after it.

Color Ramp: a gradient map. The sprite’s brightness picks a colour from a ramp texture, from left (dark) to right (light), so the whole sprite is redrawn in the ramp’s colours. With Use Editable Gradient? the ramp is a gradient in the inspector. It is the closest the shader comes to a palette swap of every colour, as long as the palette can be ordered from dark to light.

The soldier, the demo's ramp texture running from black through red and light grey to white, and the soldier redrawn in those four colours by brightness

Every one of these is a material property, so a script sets them:

private static readonly int RedSwap = Shader.PropertyToID("_ColorSwapRed");
private void SetTeam(Color teamColor)
{
GetComponent<Renderer>().material.SetColor(RedSwap, teamColor);
}

renderer.material gives this renderer its own copy, so one character changes and not all of them; a MaterialPropertyBlock does the same without a copy. Both are on Scripting, with the UI case, where Images share their material and need a copy made on Awake. Colours animate in the Animation window too (How to Animate Effects).

With other effects. Color Swap, Color Ramp, Change 1 Color and Hue Shift are applied in that order, and after Hit Effect. A white hit flash therefore passes through them: a Change 1 Color that targets white turns the flash into its new colour. The way out is to turn the swap’s Blend to 0 during the flash, or to move the Hit Effect block below them in the shader. Both are step by step in When the flash is not white.

  • More than three regions or three colours in one pass. Color Swap has three channels and Change 1 Color three slots. A palette of 16 changeable colours needs the index and palette method above, or Color Ramp if the palette runs from dark to light.
  • Swap by index. The shader matches colours or masks; it does not read a palette index from the sprite.