TweenColor.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. /// <summary>
  8. /// Tween the object's color.
  9. /// </summary>
  10. public class TweenColor : Tweener
  11. {
  12. public Color from = Color.white;
  13. public Color to = Color.white;
  14. bool mCached = false;
  15. Image mImage;
  16. Text mText;
  17. Material mMat;
  18. Light mLight;
  19. void Cache ()
  20. {
  21. mCached = true;
  22. mImage = GetComponent<Image>();
  23. mText = GetComponent<Text>();
  24. Renderer ren = GetComponent<Renderer>();
  25. if (ren != null) mMat = ren.material;
  26. mLight = GetComponent<Light>();
  27. }
  28. [System.Obsolete("Use 'value' instead")]
  29. public Color color { get { return this.value; } set { this.value = value; } }
  30. /// <summary>
  31. /// Tween's current value.
  32. /// </summary>
  33. public Color value
  34. {
  35. get
  36. {
  37. if (!mCached) Cache();
  38. if (mImage != null) return mImage.color;
  39. else if (mText != null) return mText.color;
  40. else if (mLight != null) return mLight.color;
  41. else if (mMat != null) return mMat.color;
  42. return Color.black;
  43. }
  44. set
  45. {
  46. if (!mCached) Cache();
  47. if (mImage != null) mImage.color = value;
  48. else if (mText != null) mText.color = value;
  49. else if (mMat != null) mMat.color = value;
  50. else if (mLight != null)
  51. {
  52. mLight.color = value;
  53. mLight.enabled = (value.r + value.g + value.b) > 0.01f;
  54. }
  55. }
  56. }
  57. /// <summary>
  58. /// Tween the value.
  59. /// </summary>
  60. protected override void OnUpdate (float factor, bool isFinished) { value = Color.Lerp(from, to, factor); }
  61. /// <summary>
  62. /// Start the tweening operation.
  63. /// </summary>
  64. static public TweenColor Begin (GameObject go, float duration, Color color)
  65. {
  66. #if UNITY_EDITOR
  67. if (!Application.isPlaying) return null;
  68. #endif
  69. TweenColor comp = Tweener.Begin<TweenColor>(go, duration);
  70. comp.from = comp.value;
  71. comp.to = color;
  72. if (duration <= 0f)
  73. {
  74. comp.Sample(1f, true);
  75. comp.enabled = false;
  76. }
  77. return comp;
  78. }
  79. [ContextMenu("Set 'From' to current value")]
  80. public override void SetStartToCurrentValue () { from = value; }
  81. [ContextMenu("Set 'To' to current value")]
  82. public override void SetEndToCurrentValue () { to = value; }
  83. [ContextMenu("Assume value of 'From'")]
  84. void SetCurrentValueToStart () { value = from; }
  85. [ContextMenu("Assume value of 'To'")]
  86. void SetCurrentValueToEnd () { value = to; }
  87. }