WWW网上下载管理器

思想:用队列将要下载的任务全部存起来,然后用协程依次去下载。

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

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值