Unity UGUI Slider颜色改变(过渡)



using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SliderColor : MonoBehaviour
{

    public Color[] colors = new Color[] { Color.red, Color.yellow, Color.green };
    Slider slider;   
    void Start()
    {
        slider = GetComponent();
        slider.fillRect.transform.GetComponent().color = Color.green; 
    }
    void Update()
    {
        float val = slider.value;
        val *= (colors.Length - 1);
        int startIndex = Mathf.FloorToInt(val);

        Color color = colors[0];

        if (startIndex >= 0)
        {
            if (startIndex + 1 < colors.Length)
            {
                float factor = (val - startIndex);
                color = Color.Lerp(colors[startIndex], colors[startIndex + 1], factor);
            }
            else if (startIndex < colors.Length)
            {
                color = colors[startIndex];
            }
            else color = colors[colors.Length - 1];
        }
        color.a = slider.fillRect.transform.GetComponent().color.a;
        slider.fillRect.transform.GetComponent().color = color;
    }
}