I’ve made a character with every pixel in a different color and laid out his clothing to create a template. I want this one to be a caveman, so I used the template and desired outcome should be this (the rest of the palette is for the back):
Here’s the script for color changing(map is the colorful template, sheet is caveman clothing):
public Texture2D map, sheet;
Color[] sheetPixels, mapPixels;
public static Dictionary<Color, Color> Colors = new Dictionary<Color, Color>();
void Start()
{
sheetPixels = sheet.GetPixels();
mapPixels = map.GetPixels();
for (int i = 0; i < mapPixels.Length; i++)
{
if (mapPixels[i].a != 0)
{
Colors.Add(mapPixels[i], sheetPixels[i]);
}
}
Texture2D currentTexture = GetComponent<SpriteRenderer>().sprite.texture;
for (int y = 0; y < currentTexture.height; y++)
{
for (int x = 0; x < currentTexture.width; x++)
{
Color currentColor = currentTexture.GetPixel(x, y);
if (Colors.ContainsKey(currentColor))
{
currentTexture.SetPixel(x, y, Colors[currentColor]);
}
}
}
currentTexture.Apply();
}
However, when I run it only some of the pixels get swapped. Do you have any idea why that happens and how to prevent that?