进度条加载界面有三种用途:资源解压进度,热更新下载进度还有副本场景加载进度; 不同的情况,是有不同的数据来源的。 资源解压进度:资源的数据列表; 热更新下载进度:根据files.txt 文件; 副本加载进度:场景的异步加载方法的返回值。 1.UGUI 环境设置 CanvasScaler 组件设置类型为:Scale With Screen Size; 比例设置为:1920*1080; 匹配模式:设置为匹配高度。 LoadingPanel是背景图,Loading是进度条边框图片,Center是红色填充图片,percentNum是百分数。 [

](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021101-300x208.jpg)](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021101.jpg)2.Center填充图片设置 设置为Filled填充类型,我们通过控制FILL Amount从0到1的大小来操作进度条的前进。 [![](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021102-300x174.png)](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021102.png) 3.运行效果(gif图片): [![](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021103-281x300.gif)
](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021101-300x208.jpg)](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021101.jpg)2.Center填充图片设置 设置为Filled填充类型,我们通过控制FILL Amount从0到1的大小来操作进度条的前进。 [![](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021102-300x174.png)](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021102.png) 3.运行效果(gif图片): [![](http://www.wjgbaby.com/wp-content/uploads/2018/02/18021103-281x300.gif)
代码:

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

public class LoadingPanel : MonoBehaviour {

private Transform m\_Transform;
private Image loading\_Center\_Image;
private Text loading\_num\_Text;

private float loadingValue = 0;

void Start () {
    m\_Transform = gameObject.GetComponent<Transform>();
    loading\_Center\_Image = m\_Transform.Find("Loading/Center").GetComponent<Image>();
    loading\_num\_Text = m\_Transform.Find("Loading/percentNum").GetComponent<Text>();

    StartCoroutine("SetLoadingValue");
}

/// <summary>
/// 设置进度条数值.
/// </summary>
IEnumerator SetLoadingValue()
{
    while (loadingValue < 0.95f)
    {
        loadingValue += UnityEngine.Random.Range(0.01f, 0.1f);
        Debug.Log(loadingValue);
        loading\_Center\_Image.fillAmount = loadingValue;
        loading\_num\_Text.text = Math.Round(loadingValue, 2) \* 100 + "%";

        yield return new WaitForSeconds(0.2f);
    }

    loading\_Center\_Image.fillAmount = 1;
    loading\_num\_Text.text = "100%";

    Debug.Log("进度条加载完毕.");
}

}