unity 填充量lerp

Unity fill amount lerp

正在尝试从此脚本中提取图像填充量。

public class ProgressBar : MonoBehaviour
{
    public static int minimum;
    public static int maximum;
    public static int current;
    public Image mask;


    void Update()
    {
        GetCurrentFill();
    }

    void GetCurrentFill()
    {
        float currentOffset = current - minimum;
        float maximumOffset = maximum - minimum;
        float fillAmount = currentOffset / maximumOffset;
        mask.fillAmount = fillAmount;
    }
}

我将解释这段代码:

Current = 当前值,minimum = 最小升级经验,maximum = 最大升级经验

   if(skortotal < 20)
        {
            playerlevel = 1;
            ProgressBar.minimum = 0;
            ProgressBar.current = skortotal;
            ProgressBar.maximum = 20;
        }
        if(skortotal >= 20)
        {
            playerlevel = 2;
            ProgressBar.current = skortotal;
            ProgressBar.maximum = 50;
            ProgressBar.minimum = 20;
        }
}

代码已经可以工作,但我不知道如何使用 lerp 使其工作

您询问如何使用 Mathf.Lerp 实现此目的,这是我的建议:

mask.fillAmount = Mathf.Lerp(minimum, maximum, current) / maximum;

Lerp 会自动限制值,所以结果总是在 [0..1] 内。

为了动画填充随时间你可以试试这个:

float actualValue = 0f; // the goal
float startValue = 0f; // animation start value
float displayValue = 0f; // value during animation
float timer = 0f;

// animate the value from startValue to actualValue using displayValue over time using timer. (needs to be called every frame in Update())
timer += Time.deltaTime;
displayValue = Mathf.Lerp(startValue, actualValue, timer);
mask.fillAmount = displayValue;
    

要启动动画,请在更改 actualValue 时执行此操作:

actualValue = * some new value *;
startValue = maskFillAmount; // remember amount at animation start
timer = 0f; // reset timer.