统一填充量

问题描述

尝试从此脚本中获取图像填充量。

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

我将解释此代码:

当前=当前值,最小值=升级所需的最低经验,最大值=升级所需的最高经验

   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.

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...