TweenAlphaCanvasGroup.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. /// <summary>
  4. /// Tween the object's alpha.
  5. /// </summary>
  6. [RequireComponent(typeof(CanvasGroup))]
  7. public class TweenAlphaCanvasGroup : Tweener
  8. {
  9. [Range(0f, 1f)]
  10. public float from = 1f;
  11. [Range(0f, 1f)]
  12. public float to = 1f;
  13. public bool resetOnEnable = false;
  14. CanvasGroup m_CanvasGroup;
  15. public CanvasGroup canvasGroup {
  16. get {
  17. if (m_CanvasGroup == null)
  18. m_CanvasGroup = GetComponent<CanvasGroup>();
  19. return m_CanvasGroup;
  20. }
  21. }
  22. /// <summary>
  23. /// Tween's current value.
  24. /// </summary>
  25. public float value {
  26. get {
  27. return canvasGroup.alpha;
  28. }
  29. set {
  30. canvasGroup.alpha = value;
  31. }
  32. }
  33. void OnEnable()
  34. {
  35. if (resetOnEnable)
  36. {
  37. tweenFactor = 0;
  38. Sample(0, false);
  39. }
  40. }
  41. /// <summary>
  42. /// Tween the value.
  43. /// </summary>
  44. protected override void OnUpdate(float factor, bool isFinished)
  45. {
  46. value = Mathf.Lerp(from, to, factor);
  47. }
  48. /// <summary>
  49. /// Start the tweening operation.
  50. /// </summary>
  51. static public TweenAlphaCanvasGroup Begin(GameObject go, float duration, float alpha)
  52. {
  53. TweenAlphaCanvasGroup comp = Tweener.Begin<TweenAlphaCanvasGroup>(go, duration);
  54. comp.from = comp.value;
  55. comp.to = alpha;
  56. if (duration <= 0f)
  57. {
  58. comp.Sample(1f, true);
  59. comp.enabled = false;
  60. }
  61. return comp;
  62. }
  63. public override void Play(bool forward)
  64. {
  65. base.Play(forward);
  66. }
  67. public override void SetStartToCurrentValue()
  68. {
  69. from = value;
  70. }
  71. public override void SetEndToCurrentValue()
  72. {
  73. to = value;
  74. }
  75. }