TweenAlpha.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 alpha.
  9. /// </summary>
  10. public class TweenAlpha : Tweener
  11. {
  12. #if UNITY_3_5
  13. public float from = 1f;
  14. public float to = 1f;
  15. #else
  16. [Range(0f, 1f)] public float from = 1f;
  17. [Range(0f, 1f)] public float to = 1f;
  18. #endif
  19. CanvasGroup mPanel;
  20. Image mImage;
  21. protected override void Start()
  22. {
  23. mPanel = GetComponent<CanvasGroup>();
  24. mImage = GetComponent<Image>();
  25. base.Start();
  26. }
  27. void OnEnable()
  28. {
  29. mPanel = GetComponent<CanvasGroup>();
  30. mImage = GetComponent<Image>();
  31. }
  32. /// <summary>
  33. /// Tween's current value.
  34. /// </summary>
  35. public float value
  36. {
  37. get
  38. {
  39. if (mPanel != null) return mPanel.alpha;
  40. else if (mImage != null) return mImage.color.a;
  41. return 0f;
  42. }
  43. set
  44. {
  45. if(mPanel != null) mPanel.alpha = value;
  46. else if(mImage != null)
  47. {
  48. Color col = mImage.color;
  49. col.a = value;
  50. mImage.color = col;
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Tween the value.
  56. /// </summary>
  57. protected override void OnUpdate (float factor, bool isFinished) { value = Mathf.Lerp(from, to, factor); }
  58. /// <summary>
  59. /// Start the tweening operation.
  60. /// </summary>
  61. static public TweenAlpha Begin (GameObject go, float duration, float alpha)
  62. {
  63. TweenAlpha comp = Tweener.Begin<TweenAlpha>(go, duration);
  64. comp.from = comp.value;
  65. comp.to = alpha;
  66. if (duration <= 0f)
  67. {
  68. comp.Sample(1f, true);
  69. comp.enabled = false;
  70. }
  71. return comp;
  72. }
  73. public override void SetStartToCurrentValue () { from = value; }
  74. public override void SetEndToCurrentValue () { to = value; }
  75. }