TweenAlpha.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/Tween/Tween Alpha")]
  4. public class TweenAlpha : UITweener
  5. {
  6. [Obsolete("Use 'value' instead")]
  7. public float alpha
  8. {
  9. get
  10. {
  11. return this.value;
  12. }
  13. set
  14. {
  15. this.value = value;
  16. }
  17. }
  18. private void Cache()
  19. {
  20. this.mCached = true;
  21. this.mRect = base.GetComponent<UIRect>();
  22. this.mSr = base.GetComponent<SpriteRenderer>();
  23. if (this.mRect == null && this.mSr == null)
  24. {
  25. Renderer component = base.GetComponent<Renderer>();
  26. if (component != null)
  27. {
  28. this.mMat = component.material;
  29. }
  30. if (this.mMat == null)
  31. {
  32. this.mRect = base.GetComponentInChildren<UIRect>();
  33. }
  34. }
  35. }
  36. public float value
  37. {
  38. get
  39. {
  40. if (!this.mCached)
  41. {
  42. this.Cache();
  43. }
  44. if (this.mRect != null)
  45. {
  46. return this.mRect.alpha;
  47. }
  48. if (this.mSr != null)
  49. {
  50. return this.mSr.color.a;
  51. }
  52. return (!(this.mMat != null)) ? 1f : this.mMat.color.a;
  53. }
  54. set
  55. {
  56. if (!this.mCached)
  57. {
  58. this.Cache();
  59. }
  60. if (this.mRect != null)
  61. {
  62. this.mRect.alpha = value;
  63. }
  64. else if (this.mSr != null)
  65. {
  66. Color color = this.mSr.color;
  67. color.a = value;
  68. this.mSr.color = color;
  69. }
  70. else if (this.mMat != null)
  71. {
  72. Color color2 = this.mMat.color;
  73. color2.a = value;
  74. this.mMat.color = color2;
  75. }
  76. }
  77. }
  78. protected override void OnUpdate(float factor, bool isFinished)
  79. {
  80. this.value = Mathf.Lerp(this.from, this.to, factor);
  81. }
  82. public static TweenAlpha Begin(GameObject go, float duration, float alpha)
  83. {
  84. TweenAlpha tweenAlpha = UITweener.Begin<TweenAlpha>(go, duration);
  85. tweenAlpha.from = tweenAlpha.value;
  86. tweenAlpha.to = alpha;
  87. if (duration <= 0f)
  88. {
  89. tweenAlpha.Sample(1f, true);
  90. tweenAlpha.enabled = false;
  91. }
  92. return tweenAlpha;
  93. }
  94. public override void SetStartToCurrentValue()
  95. {
  96. this.from = this.value;
  97. }
  98. public override void SetEndToCurrentValue()
  99. {
  100. this.to = this.value;
  101. }
  102. [Range(0f, 1f)]
  103. public float from = 1f;
  104. [Range(0f, 1f)]
  105. public float to = 1f;
  106. private bool mCached;
  107. private UIRect mRect;
  108. private Material mMat;
  109. private SpriteRenderer mSr;
  110. }