简单动态进度条

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

public class ui_MainPanel_shengyin : MonoBehaviour {

private Image image;

/// <summary>
/// 长度
/// </summary>
public int value = 50;

/// <summary>
/// 精度条格子宽度
/// </summary>
private float width;

/// <summary>
/// 进度条格子长度
/// </summary>
private float height;

/// <summary>
/// 整个精度条一半长度
/// </summary>
private float Halfwidth;

// Use this for initialization
void Start () {
    image = Resources.Load<Image>("Prefabs/UI/tiao");

    width = GetComponent<RectTransform>().sizeDelta.x / value;//获取单个格子宽
    height = GetComponent<RectTransform>().sizeDelta.y;//获取单个格子高
    Halfwidth = GetComponent<RectTransform>().sizeDelta.x / 2;

    lengthList.Add(0);
    lengthList.Add(0);

}

float AllTimer = 1.5f;//总时间

float timer =0;//单个时间
float runTimer = 0;//运行时间
List<int> lengthList = new List<int>();

// Update is called once per frame
void Update () {

    if (lengthList[0]!=lengthList[1])
    {
        if (lengthList[0] == lengthList[1])
        {
            return;
        }
        if (runTimer>=timer)
        {
            runTimer = 0;

            if (lengthList[0]<lengthList[1])
            {
                lengthList[0]++;
                ShowImage(lengthList[0],true);
            }
            else if (lengthList[0]>lengthList[1])
            {
                ShowImage(lengthList[0], false);
                lengthList[0]--;
            }
        }
        else
        {
            runTimer +=Time.deltaTime;
        }



    }



}

List<Image> imageList = new List<Image>();

/// <summary>
/// 设置时间
/// </summary>
public void SetTimer(int length)
{
    if (length>value)
    {
        length = value;
    }
    if (length<0)
    {
        length = 0;
    }
    lengthList[1] = length;

    if (lengthList[1]==lengthList[0])
    {
        return;
    }
    timer = AllTimer / (Mathf.Abs(lengthList[1] - lengthList[0]));//让精度条在规定时间内完成

}

/// <summary>
/// 显示进度条
/// </summary>
/// <param name="num"></param>
public void ShowImage(int num,bool bo)
{
    Image mag = null;
    if (imageList.Count > num)
    {
        mag = imageList[num];
        mag.gameObject.SetActive(true);
    }
    else
    {
        mag = Instantiate(this.image);
        mag.transform.SetParent(this.transform);
        imageList.Add(mag);
    }

    mag.rectTransform.anchoredPosition3D = new Vector3(-Halfwidth + num * width, 0, 0);//设置位置
    mag.transform.localScale = Vector3.one;//设置图片大小
                                           // mag.rectTransform.sizeDelta=new Vector2(width-0.5f,height);
    mag.gameObject.SetActive(bo);

}

}

直接挂在ui image下

效果图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值