Hi readers!

    This is the first post tagged with #ft tag, so I will do a little explanation about it before start with the tutorial: This tag will be in tutorials that they explains little tips or fast things, so they will be short and useful! Amazing!

    OK! Now, we can start with the tutorial so let's go! How to do a gradient bar? A gradient bar is used in games for score, health... currently I am using the following code in the development of a game, so if you have any problem, post a comment and I will answer you as soon I can.

    Our objetive is do this:

http://codenauta.blogspot.com

    We only need to follow few steps:
  1. Create a plane and scale it to seem like a progress bar
  2. Create a new script called GradientBar ,paste the code posted at the end of the post, save the file and add this script to the plane
  3. Press Play Button to start debug, and press Space Key to show our amazing Gradient Bar!
    If you have any problem or you want to say something post a comment or say something in my Twitter (@Mr_Nauta).
    Here is the code of GradientBar script:


//Code created by @Mr_Nauta and posted in http://codenauta.blogspot.com
//If you have any question please ask me in a comment or ask me in my Twitter: @Mr_Nauta
//You can use this code in your project but I like that I will appear in credits or thanks :)

using UnityEngine;
using System.Collections;

public class GradientBar : MonoBehaviour {
 private Texture2D bar_tex;//This is the texture of the progress bar

 int c=100;//This variable is only for testing, while is pressed space key it will decreased by 1 (see Update() function)

 void Start () {
  //Create a new texture (100 width, 1 height)
  bar_tex = new Texture2D (100, 1);
  //With these options the texture will be automatically resized without code
  bar_tex.filterMode = FilterMode.Bilinear;
  bar_tex.wrapMode = TextureWrapMode.Clamp;
  //Apply changes in the texture
  bar_tex.Apply();
  //And set this texture as the main texture of our progress bar (a basic plane object)
  renderer.material.mainTexture = bar_tex;
 }

 //This function is only used for testing and can be deleted
 void Update () {
  //If is pressed the space key...
  if (Input.GetKey("space")) {
   //If c is more than 100...
   if(c>100)
    //We set it to 100
    c=100;
   //If c is between 100 and 0...
   if((c<=100)&&(c>-1)){
    //Set pixel at coordinates c,1 (c is the width and 1 the height (height is constant because our texture is only 1 of height))
    bar_tex.SetPixel(c,1,GetBlendedColor(c));//GetBlendedColor is our main function and only need the percentage of the progress bar in base 100 like 9% or 21%
    bar_tex.Apply(); //Apply changes
    c--;//And decrese by 1 c variable
   }
  }
 }
 public Color GetBlendedColor(int percentage){
  //If percentage is less than 50
  if (percentage < 50) {
   //Call interpolate function between colors green and yellow, and parsing the total percentage of the progress bar to a custom percentage (see our TransfromPercentage function for more info)
   return Interpolate(Color.green,Color.yellow,TransformPercentage(percentage));
  } else {
   //If is more than 50 call interpolate function with colors yellow and red
   return Interpolate(Color.red,Color.yellow,TransformPercentage(100-percentage));
  }
 }
 public float TransformPercentage(int p){
  //This function divide our 100 per 100 progress bar in 2 "virtual" progress bar:
  //-> 1 progress bar of 100 point between 0 and 50
  //-> Other progress bar of 100 point between 50 and 100
  //And also transform that value in a float value, transforming the value from base 100 to base 1, for example:
  //In our 100 per 100 progress bar we have this percentage: 23%
  //Our final value will be: (23/100.0)*2.0
  //Other example, in our 100 per 100 progress bar we have this percentage: 86%
  //Our final value will be: ((100-86)/100.0)*2.0
  return (float)((p / 100f) * 2f);
 }
 public Color Interpolate(Color c1,Color c2,float p){
  //This function use a Unity function called color lerp that need three arguments;
  //c1 and c2 (interpolated colors)
  //p The percentage of interpolation in our situation
  return Color.Lerp(c1,c2,p);
 }
}

If you prefer, I have done a Unity Package with an example scene and this script that you can download in the following link and test it: