簡體   English   中英

unity 填充量 lerp

[英]Unity fill amount lerp

試圖從這個腳本 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;
    }
}

我將解釋這段代碼:

當前 = 當前值,最小值 = 升級的最小經驗,最大值 = 升級的最大經驗

   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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM