TweenFillAmount.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. [RequireComponent(typeof(Image))]
  5. public class TweenFillAmount : Tweener
  6. {
  7. public float from = 1.0f;
  8. public float to = 1.0f;
  9. private Image mImage;
  10. public Image cachedImage { get { return mImage; } }
  11. public float value { get { return mImage.fillAmount; } set { mImage.fillAmount = value; } }
  12. private void Awake()
  13. {
  14. mImage = GetComponent<Image>();
  15. }
  16. protected override void OnUpdate(float factor, bool isFinished)
  17. {
  18. value = from * (1f - factor) + to * factor;
  19. }
  20. public static TweenFillAmount Begin(GameObject go, float duration, float to)
  21. {
  22. TweenFillAmount comp = Tweener.Begin<TweenFillAmount>(go, duration);
  23. comp.from = comp.value;
  24. comp.to = to;
  25. if (duration <= 0f)
  26. {
  27. comp.Sample(1f, true);
  28. comp.enabled = false;
  29. }
  30. return comp;
  31. }
  32. [ContextMenu("Set 'From' to current value")]
  33. public override void SetStartToCurrentValue() { from = value; }
  34. [ContextMenu("Set 'To' to current value")]
  35. public override void SetEndToCurrentValue() { to = value; }
  36. [ContextMenu("Assume value of 'From'")]
  37. void SetCurrentValueToStart() { value = from; }
  38. [ContextMenu("Assume value of 'To'")]
  39. void SetCurrentValueToEnd() { value = to; }
  40. }