UISliderColors.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Examples/Slider Colors")]
  4. public class UISliderColors : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. this.mBar = base.GetComponent<UIProgressBar>();
  9. this.mSprite = base.GetComponent<UIBasicSprite>();
  10. this.Update();
  11. }
  12. private void Update()
  13. {
  14. if (this.sprite == null || this.colors.Length == 0)
  15. {
  16. return;
  17. }
  18. float num = (!(this.mBar != null)) ? this.mSprite.fillAmount : this.mBar.value;
  19. num *= (float)(this.colors.Length - 1);
  20. int num2 = Mathf.FloorToInt(num);
  21. Color color = this.colors[0];
  22. if (num2 >= 0)
  23. {
  24. if (num2 + 1 < this.colors.Length)
  25. {
  26. float t = num - (float)num2;
  27. color = Color.Lerp(this.colors[num2], this.colors[num2 + 1], t);
  28. }
  29. else if (num2 < this.colors.Length)
  30. {
  31. color = this.colors[num2];
  32. }
  33. else
  34. {
  35. color = this.colors[this.colors.Length - 1];
  36. }
  37. }
  38. color.a = this.sprite.color.a;
  39. this.sprite.color = color;
  40. }
  41. public UISprite sprite;
  42. public Color[] colors = new Color[]
  43. {
  44. Color.red,
  45. Color.yellow,
  46. Color.green
  47. };
  48. private UIProgressBar mBar;
  49. private UIBasicSprite mSprite;
  50. }