Hi, guys:
I need to create a graphical display for a variable in my game. This variable (energy) is decreasing over time so i want a colored label indicating the level of energy. The variable is controlled by a script (charscript) in the char. So i created a texture bitmap which is a green-red gradient. This variable goes from 100 (total green) to 1 (total red) so i’ve been watching around and i’ve come to this:
public Texture2D gradbitmap;
public charscript cs;
public float energygradient;
cs = (charscript)GameObject.Find("Char").GetComponent<charscript>();
OnGUI(){
energygradient=cs.energy;
GUI.DrawTextureWithTexCoords(new Rect(500,80,20,20), gradbitmap, new Rect(0f,0f,energygradient,1f));
}
The problem is energygradient will draw a percent of the bitmap into the label, so if it is 0.1 it will draw 10% of the left side of the bitmap, -0.1 10% of the right side of the bitmap, 0.2 20% of the left side of the map and so on. But the variable decreases from 100 to 1. I want the label shows only 10% of the bitmap, but i need it to “move” along the bitmap, first 10%, then from 1% to 11%, 2% to 12% … and finally 90% to 100%. I mean to create a “window” on the gradient texture so it looks like the GUI object becomes green to red in a soft way.
I can’t find a math solution for this. Do you know any other way to achieve it?