简体   繁体   English

unity 填充量 lerp

[英]Unity fill amount lerp

Trying to lerp image fill amount from this script.试图从这个脚本 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;
    }
}

i'm going to explain this code :我将解释这段代码:

Current = current value, minimum = minimum experience to level up, maximum = maximum experience to level up当前 = 当前值,最小值 = 升级的最小经验,最大值 = 升级的最大经验

   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;
        }
}

the code already work but i don't know how to make it work using lerp代码已经工作,但我不知道如何使用 lerp 使其工作

You asked how to achieve this using Mathf.Lerp , here is my suggestion:你问如何使用Mathf.Lerp实现这一点,这是我的建议:

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

Lerp will automatically clamp the values, so the result is always inside of [0..1]. Lerp 会自动钳制这些值,所以结果总是在 [0..1] 之内。

To animate the fill over time you can try this:随着时间的推移填充动画,您可以尝试以下操作:

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;
    

To start the animation, do this when you change the actualValue:要开始动画,请在更改 actualValue 时执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM