Unity中實現在規定時間內從一個值遞增到另一個值


1、進度條(在規定時間內完成進度條)

    private Image progressBar;
    private float currentProgress = 0;
    /// <summary>
    /// 進度條需要持續的時長
    /// </summary>
    private float duration = 5f;//這個時間可以任意定義

    private void ProgressBar()
    {
        currentProgress += Time.deltaTime;
        progressBar.fillAmount = currentProgress / duration;
        if (currentProgress >= duration)
        {
            progressBar.fillAmount = 1;
        }
    }
View Code

2、數值遞增(在規定時間內從0遞增到115)

    private Text showText;//展示的Text
    private float time = 5f;//指定的時間
    private float currentVaule = 0f;//起始值
    private float targetVaule = 115f;//目標值
    /// <summary>
    /// 數據遞增
    /// </summary>
    public void DataIncrement()
    {
        currentVaule += Time.deltaTime * (targetVaule / time);

        if (currentVaule >= targetVaule)
        {
            currentVaule = targetVaule;
        }
        showText.text = currentVaule.ToString("0000.00");
    }
View Code

 

注:以上方法需放在Update中調用執行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM