Reputation:
I'd like to be able to create a color gradient bar using C# script too look like this:
Then I'd like to say if my number is in range (0.8 - 1.0) make my object that shade of red that's shown in the gradient.
I'm not sure how to approach this. For now I just have 2 solid colors. I have:
myGameObject.GameComponent<Renderer>().material.color = Color.red;
for positive values and blue for negative values.
Any help would be appreciated.
Upvotes: 5
Views: 22937
Reputation: 612
See Unity's Gradient
class. You can create a list of GradientColorKey[]
and GradientAlphaKey
- and set each color/time value. For your purpose, I would use:
Gradient g = new Gradient();
GradientColorKey[] gck = new GradientColorKey[2];
GradientAlphaKey[] gak = new GradientAlphaKey[2];
gck[0].color = Color.red;
gck[0].time = 1.0F;
gck[1].color = Color.blue;
gck[1].time = -1.0F;
gak[0].alpha = 0.0F;
gak[0].time = 1.0F;
gak[1].alpha = 0.0F;
gak[1].time = -1.0F;
g.SetKeys(gck, gak);
myGameObject.GameComponent<Renderer>().material.color = g.Evaluate(1);
This will get you the Color
that corresponds to the number in g.Evaluate()
so you should change the time parameter in g.Evaluate(1)
to a variable or whatever you want. If you're getting transparent or black textures, try changing the alpha
values in gak
. Hope this was helpful :)
Upvotes: 6