Unity中实现在规定时间内从一个值递增到另一个值

时间:2021-06-24 18:09:17   收藏:0   阅读:0

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)
        {
            showText.text = currentVaule.ToString("0000.00");
        }
        else
        {
            currentVaule = targetVaule;
            showText.text = currentVaule.ToString("0000.00");
        }
   
View Code

 

 

注:以上方法需放在Update中调用执行

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!