How to gradient-wise fill a bitmap programmatically

I’ll be using this for GUI purposes, to allow the user customization of colors of the GUI, the HP bars and other gauges are Gradients. using Set pixels a millions times in a loop is far too slow. I want the Player to define two colors using RGB sliders, with normal GUI i can just tint the color setting but this will not work with gradients.

is there any efficient way i could achieve this? Its similar to the FFVII window background color customization but an overlay of what already there.

Why would you use SetPixel millions of times in a loop? A gradient texture is just 2 pixels.

I tried using 2 pixels, but when I stretched to about 180x16 pixels you'd have two separate rectangles of solid color. @Fattie So using an overlay of two gradients? that seems logical. so all i need is two black/gray to blank gradients, and adjust there color.

No, you just use bilinear filtering, and 2 pixels can be stretched to anything and will result in a smooth gradient.

What a great tip !!!!!!!!!!!

1 Answer

1

This seems to do the trick.

 Texture2D a = new Texture2D(2,1);
                a.SetPixel(1, 1, color1);
                a.SetPixel(2, 1, color2);
                a.filterMode = FilterMode.Bilinear;
                a.wrapMode = TextureWrapMode.Clamp;
                a.Apply();
                GUI.DrawTexture(new Rect(x, gague_y, width, 16), a);