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);
}
}