思想:用队列将要下载的任务全部存起来,然后用协程依次去下载。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//下载 开始 完成 错误
public delegate void DownLoadBegin(WWW www);
public delegate void DownLoadFinish(WWW www);
public delegate void DownLoadError(WWW www, WWWTask task);
/// <summary>
/// 下载类型
/// </summary>
public enum DownLoadType
{
Get,
Post,
}
/// <summary>
/// 下载类
/// </summary>
public class WWWTask
{
//地址
public string URL { get; set; }
public DownLoadBegin downLoadBeginDelegate;
public DownLoadFinish downLoadFinishDelegate;
public DownLoadError downLoadErrorDelegate;
/// <summary>
/// 下载类型
/// </summary>
public DownLoadType type;
public WWWForm form;
public WWWTask(
string url,
DownLoadType type,
WWWForm form,
DownLoadBegin begionFunc,
DownLoadFinish finishFunc,
DownLoadError erroeFunc
)
{
URL = url;
this.form = form;
downLoadBeginDelegate = begionFunc;
downLoadFinishDelegate = finishFunc;
downLoadErrorDelegate = erroeFunc;
}
public WWWTask(
string url,
DownLoadType type,
DownLoadBegin begionFunc,
DownLoadFinish finishFunc,
DownLoadError erroeFunc
)
{
URL = url;
downLoadBeginDelegate = begionFunc;
downLoadFinishDelegate = finishFunc;
downLoadErrorDelegate = erroeFunc;
}
public IEnumerator DownLoad()
{
WWW www;
switch (type)
{
case DownLoadType.Get:
www = new WWW(URL);
break;
case DownLoadType.Post:
www = new WWW(URL, this.form);
break;
default:
www = new WWW("http://www.baidu.com");
Debug.LogError("Null");
break;
}
Debug.Log(downLoadBeginDelegate != null);
//开始下载
if (downLoadBeginDelegate != null)
{
Debug.Log("开始下载");
downLoadBeginDelegate(www);
}
yield return www;
//下载完后通知用户
if (string.IsNullOrEmpty(www.error))
{
if (downLoadFinishDelegate != null)
{
Debug.Log("下载完成");
downLoadFinishDelegate(www);
}
else
{
if (downLoadErrorDelegate != null)
{
downLoadErrorDelegate(www, this);
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WWWControl : MonoSingleton<WWWControl>
{
Queue<WWWTask> downLoad = new Queue<WWWTask>();
/// <summary>
/// 支持最大并发数
/// </summary>
private int MaxCount = 2;
/// <summary>
/// 添加任务方法
/// </summary>
private int CurrCount = 0;
/// <summary>
/// 是否在下载
/// </summary>
bool allFinish = true;
/// <summary>
/// 添加任务到队列
/// </summary>
/// <param name="task"></param>
public void AddTask(WWWTask task)
{
downLoad.Enqueue(task);
if (allFinish)
{
StartCoroutine(DownLoadQueue());
}
}
private IEnumerator DownLoadQueue()
{
allFinish = false;
while (downLoad.Count > 0)
{
if (CurrCount < MaxCount)
{
CurrCount++;
WWWTask task = downLoad.Dequeue();
task.downLoadFinishDelegate += delegate (WWW www)
{
CurrCount--;
};
StartCoroutine(task.DownLoad());
}
yield return null;
}
allFinish = true;
}
}
调用脚本:
public RawImage r1;
public RawImage r2;
public RawImage r3;
string url_1 = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1527932906903&di=5c3e65d929f6e3eb2a98dddfba6e8f37&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F9213b07eca806538805643379ddda144ad348252.jpg";
string url_2 = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1527933190721&di=b2d7b904dc91b8f9de13f03bc12adfad&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F5fdf8db1cb134954bf5d3d465c4e9258d1094a55.jpg";
private void Start()
{
WWWTask task1 = new WWWTask(url_1, DownLoadType.Get, Demo1, delegate (WWW www)
{
r1.texture = www.texture;
r1.SetNativeSize();
}, null
);
WWWTask task2 = new WWWTask(url_2, DownLoadType.Get, Demo1, delegate (WWW www)
{
r2.texture = www.texture;
r2.SetNativeSize();
}, null
);
WWWTask task3 = new WWWTask(url_2, DownLoadType.Get, delegate (WWW www) { Debug.Log("开始 1111 下载"); }, delegate (WWW www)
{
r3.texture = www.texture;
r3.SetNativeSize();
}, null
);
WWWControl.Instance.AddTask(task1);
WWWControl.Instance.AddTask(task2);
WWWControl.Instance.AddTask(task3);
}
public void Demo1(WWW ww)
{
Debug.Log("调用Demo1");
}