TweenColor.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Tween/Tween Color")]
  4. public class TweenColor : UITweener
  5. {
  6. private void Cache()
  7. {
  8. this.mCached = true;
  9. this.mWidget = base.GetComponent<UIWidget>();
  10. if (this.mWidget != null)
  11. {
  12. return;
  13. }
  14. this.mSr = base.GetComponent<SpriteRenderer>();
  15. if (this.mSr != null)
  16. {
  17. return;
  18. }
  19. Renderer component = base.GetComponent<Renderer>();
  20. if (component != null)
  21. {
  22. this.mMat = component.material;
  23. return;
  24. }
  25. this.mLight = base.GetComponent<Light>();
  26. if (this.mLight == null)
  27. {
  28. this.mWidget = base.GetComponentInChildren<UIWidget>();
  29. }
  30. }
  31. [Obsolete("Use 'value' instead")]
  32. public Color color
  33. {
  34. get
  35. {
  36. return this.value;
  37. }
  38. set
  39. {
  40. this.value = value;
  41. }
  42. }
  43. public Color value
  44. {
  45. get
  46. {
  47. if (!this.mCached)
  48. {
  49. this.Cache();
  50. }
  51. if (this.mWidget != null)
  52. {
  53. return this.mWidget.color;
  54. }
  55. if (this.mMat != null)
  56. {
  57. return this.mMat.color;
  58. }
  59. if (this.mSr != null)
  60. {
  61. return this.mSr.color;
  62. }
  63. if (this.mLight != null)
  64. {
  65. return this.mLight.color;
  66. }
  67. return Color.black;
  68. }
  69. set
  70. {
  71. if (!this.mCached)
  72. {
  73. this.Cache();
  74. }
  75. if (this.mWidget != null)
  76. {
  77. this.mWidget.color = value;
  78. }
  79. else if (this.mMat != null)
  80. {
  81. this.mMat.color = value;
  82. }
  83. else if (this.mSr != null)
  84. {
  85. this.mSr.color = value;
  86. }
  87. else if (this.mLight != null)
  88. {
  89. this.mLight.color = value;
  90. this.mLight.enabled = (value.r + value.g + value.b > 0.01f);
  91. }
  92. }
  93. }
  94. protected override void OnUpdate(float factor, bool isFinished)
  95. {
  96. this.value = Color.Lerp(this.from, this.to, factor);
  97. }
  98. public static TweenColor Begin(GameObject go, float duration, Color color)
  99. {
  100. TweenColor tweenColor = UITweener.Begin<TweenColor>(go, duration);
  101. tweenColor.from = tweenColor.value;
  102. tweenColor.to = color;
  103. if (duration <= 0f)
  104. {
  105. tweenColor.Sample(1f, true);
  106. tweenColor.enabled = false;
  107. }
  108. return tweenColor;
  109. }
  110. [ContextMenu("Set 'From' to current value")]
  111. public override void SetStartToCurrentValue()
  112. {
  113. this.from = this.value;
  114. }
  115. [ContextMenu("Set 'To' to current value")]
  116. public override void SetEndToCurrentValue()
  117. {
  118. this.to = this.value;
  119. }
  120. [ContextMenu("Assume value of 'From'")]
  121. private void SetCurrentValueToStart()
  122. {
  123. this.value = this.from;
  124. }
  125. [ContextMenu("Assume value of 'To'")]
  126. private void SetCurrentValueToEnd()
  127. {
  128. this.value = this.to;
  129. }
  130. public Color from = Color.white;
  131. public Color to = Color.white;
  132. private bool mCached;
  133. private UIWidget mWidget;
  134. private Material mMat;
  135. private Light mLight;
  136. private SpriteRenderer mSr;
  137. }