天天看点

unity3dUGUI进度条缓慢加减

unity3dUGUI进度条缓慢加减

测试.png

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class ProcessBar : MonoBehaviour
{

    public Image processBar;
    public Button Add;
    public Button Less;
    public float speed = 0.05f;
    public float curValue = 0.2f;
    public float barBegin;
    public float barEnd;
    public bool isRun = false;
    public bool isAdd = false;
    void Awake()
    {
        Add.onClick.AddListener(OnAddBar);
        Less.onClick.AddListener(OnLessBar);
        processBar.fillAmount = 0.2f;
    }

    private void OnAddBar()
    {
        Debug.LogError("+");
        if (processBar.fillAmount >= 1)
            return;
        barBegin = processBar.fillAmount;
        barEnd = processBar.fillAmount + curValue;
        isAdd = true;
        isRun = true;

    }
    private void OnLessBar()
    {
        Debug.LogError("-");
        if (processBar.fillAmount <= 0)
            return;
        barBegin = processBar.fillAmount;
        barEnd = processBar.fillAmount - curValue;
        isAdd = false;
        isRun = true;
    }

    void Update()
    {

        if (isRun)
        {
            float ratio = Time.deltaTime * speed;
            if (isAdd)
            {
                processBar.fillAmount += ratio;
                if (processBar.fillAmount >= barEnd)
                {
                    isRun = false;
                }
            }
            else
            {
                processBar.fillAmount -= ratio;
                if (processBar.fillAmount <= barEnd)
                {
                    isRun = false;
                }
            }


        }


    }




}

           

继续阅读