static void Paletteize(string fileIn, string fileOut, bool debug) { var tex = AssetDatabase.LoadAssetAtPath <Texture2D>(fileIn); if (tex == null) { Debug.Log($"bad path: {fileIn}"); return; } Dictionary <Color24, Color32> indices = new Dictionary <Color24, Color32>(); HashSet <Color24> colors = new HashSet <Color24>(); for (int i = 0; i < palette.Length; ++i) { Color24 col = palette[i]; colors.Add(col); float tx = (i + .5f) / 8f; byte b = (byte)(tx * 255); indices.Add(col, new Color32(b, b, b, 255)); } Color32[] pixels = tex.GetPixels32(); for (int i = 0; i < pixels.Length; ++i) { Color32 p = pixels[i]; Color24 col = p.Col24(); if (p.a == 0) { pixels[i] = new Color32(0, 0, 0, 0); } else { Color32 colOut; if (debug) { if (colors.Contains(col)) { colOut = new Color32(col.r, col.g, col.b, 255); } else { colOut = new Color32(255, 0, 255, 255); } } else { if (!colors.Contains(col)) { col = FindClosest(col, colors); } colOut = indices[col]; } pixels[i] = colOut; } } var texNew = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false); texNew.SetPixels32(pixels); var bytes = texNew.EncodeToPNG(); string filename = fileOut; if (debug) { filename += "-debug.png"; } File.WriteAllBytes(filename, bytes); }