public void ReplaceColor(Sprite sprite, Color from, Color to) { Texture2D texture = sprite.texture; Rect textureRect = sprite.textureRect; int minX = (int)textureRect.xMin; int maxX = (int)textureRect.xMax; int minY = (int)textureRect.yMin; int maxY = (int)textureRect.yMax; int index = 0; for (int y = minY; y < maxY; y++) { for (int x = minX; x < maxX; x++) { if (Utility.SameColor(_mSnapshot[index++], from)) { texture.SetPixel(x, y, to); } } } texture.Apply(); SceneView.RepaintAll(); }
private static void FloodFill(Color oldColor, Color color, Texture2D tex, int fX, int fY, int minX, int minY, int maxX, int maxY) { if (Utility.SameColor(oldColor, color)) //just in case. { return; } int width = maxX - minX; int height = maxY - minY; Color[] colors = tex.GetPixels(minX, minY, width, height); //store the colors into a temporary buffer Stack <Texel> stack = new Stack <Texel> (); //non-recursive stack stack.Push(new Texel(fX, fY)); //original target while (stack.Count > 0) { Texel n = stack.Pop(); int index = (n.Y - minY) * width + (n.X - minX); //index into temporary buffer bool pixelIsInTheSprite = n.X >= minX && n.X < maxX && n.Y >= minY && n.Y < maxY; if (pixelIsInTheSprite) { bool colorMatches = Utility.SameColor(colors[index], oldColor); if (colorMatches) { colors[index] = color; stack.Push(n + new Texel(-1, 0)); // stack.Push(n + new Texel(1, 0)); // add to stack in all 4 directions stack.Push(n + new Texel(0, 1)); // stack.Push(n + new Texel(0, -1)); // } } } tex.SetPixels(minX, minY, width, height, colors); //put the temporary buffer back into the texture }