Files
plugin-library/Assets/06.UIFarme/RunTime/UIFramework/Tweening/Tweening/TweenFill.cs
2025-09-23 11:18:38 +08:00

70 lines
1.3 KiB
C#

using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Tween the Image fill.
/// </summary>
[RequireComponent(typeof(Image))]
[AddComponentMenu("Tween/Tween Fill")]
public class TweenFill : UITweener
{
[Range(0f, 1f)] public float from = 1f;
[Range(0f, 1f)] public float to = 1f;
bool mCached = false;
Image mSprite;
void Cache ()
{
mCached = true;
mSprite = GetComponent<Image>();
}
/// <summary>
/// Tween's current value.
/// </summary>
public float value
{
get
{
if (!mCached) Cache();
if (mSprite != null) return mSprite.fillAmount;
return 0f;
}
set
{
if (!mCached) Cache();
if (mSprite != null) mSprite.fillAmount = value;
}
}
/// <summary>
/// Tween the value.
/// </summary>
protected override void OnUpdate (float factor, bool isFinished) { value = Mathf.Lerp(from, to, factor); }
/// <summary>
/// Start the tweening operation.
/// </summary>
static public TweenFill Begin (GameObject go, float duration, float fill)
{
TweenFill comp = UITweener.Begin<TweenFill>(go, duration);
comp.from = comp.value;
comp.to = fill;
if (duration <= 0f)
{
comp.Sample(1f, true);
comp.enabled = false;
}
return comp;
}
public override void SetStartToCurrentValue () { from = value; }
public override void SetEndToCurrentValue () { to = value; }
}