using UnityEngine; using UnityEngine.UI; /// /// Tween the object's alpha. /// [RequireComponent(typeof(Image))] public class TweenFillAmount : Tweener { [Range(0f, 1f)] public float from = 1f; [Range(0f, 1f)] public float to = 1f; public bool resetOnEnable = false; Image mGraphic; /// /// Tween's current value. /// public float value { get { if (mGraphic == null) mGraphic = GetComponent(); return mGraphic.fillAmount; } set { if (mGraphic == null) mGraphic = GetComponent(); mGraphic.fillAmount = value; } } protected override void Awake() { base.Awake(); mGraphic = GetComponent(); } void OnEnable() { if (resetOnEnable) { tweenFactor = 0; Sample(0, false); } } /// /// Tween the value. /// protected override void OnUpdate(float factor, bool isFinished) { value = Mathf.Lerp(from, to, factor); } /// /// Start the tweening operation. /// static public TweenFillAmount Begin(GameObject go, float duration, float alpha) { TweenFillAmount comp = Tweener.Begin(go, duration); comp.from = comp.value; comp.to = alpha; if (duration <= 0f) { comp.Sample(1f, true); comp.enabled = false; } return comp; } public override void SetStartToCurrentValue() { from = value; } public override void SetEndToCurrentValue() { to = value; } }