TweenFillAmount.cs 1.7 KB

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