Unity中 基于UnityWebRequest网络下载器的封装

UnityWebRequestManager:

/*********************************************
 *
 *   Title: UnityWebRequest下载管理器
 *
 *   Description: 统一获取需要请求的Web数据,用法很简单,只需要去声明 UnityWebRequestTask task = new UnityWebRequestTask(传递参数) 然后 AddTask(task)
 * 
 *   Author: 
 *
 *   Date: 2019.7.1
 *
 *   Modify: 
 * 
 *********************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
public class UnityWebRequestManager : MonoBehavior
{
    /// <summary>
    /// 下载任务队列
    /// </summary>
    private Queue<UnityWebRequestTask> m_TaskQue = new Queue<UnityWebRequestTask>();
    /// <summary>
    /// 最大并发数
    /// </summary>
    private int m_MaxCount = 2;
    /// <summary>
    /// 当前下载数
    /// </summary>
    private int m_CurrentCount = 0;
    /// <summary>
    /// 是否下载完成
    /// </summary>
    private bool m_AllFinish = true;

    public static UnityWebRequestManager Instance;

    void Awake()
    {
        Instance = this;
    }
    public void AddTask(UnityWebRequestTask task)
    {
        m_TaskQue.Enqueue(task);
        if (m_AllFinish)
        {
            StartCoroutine(DownLoadQueue());
        }
    }
    /// <summary>
    /// 开始下载任务
    /// </summary>
    /// <returns></returns>
    IEnumerator DownLoadQueue()
    {
        m_AllFinish = false;
        while (m_TaskQue.Count>0)
        {
            if (m_CurrentCount<m_MaxCount)
            {
                m_CurrentCount++;
                UnityWebRequestTask task = m_TaskQue.Dequeue();
                task.m_DownLoadFinish += delegate (UnityWebRequest webRequest)
                  {
                      m_CurrentCount--;
                  };
                StartCoroutine(task.DownLoad());
            }
            yield return null;
        }
        m_AllFinish = true;
    }
}

UnityWebRequestTask:目前已知的常用类型为Post,Get

/*********************************************
 *
 *   Title: UnityWebRequest下载列表
 *
 *   Description: 封装 控制UnityWebRequest 下载的Post请求 Get请求
 *
 *   Author:
 *
 *   Date: 2019
 *
 *   Modify: 
 * 
 *********************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public enum DownLoadType
{
    NONE,
    GET,
    POST,
    POSTMULTIPART
}
public class UnityWebRequestTask
{
    /// <summary>
    /// 下载开始
    /// </summary>
    private Action<UnityWebRequest> m_DownLoadBegin;
    /// <summary>
    /// 下载结束
    /// </summary>
    public Action<UnityWebRequest> m_DownLoadFinish;
    /// <summary>
    /// 下载错误
    /// </summary>
    private Action<UnityWebRequest, UnityWebRequestTask> m_DownLoadError;
    /// <summary>
    /// 下载类型
    /// </summary>
    private DownLoadType m_DownLoadType;
    /// <summary>
    /// 下载地址
    /// </summary>
    private string m_Url { get; set; }

    private WWWForm m_WWWForm;
    /// <summary>
    /// 表单参数  和 WWWForm 用法类似
    /// 使用方式:
    /// m_FormSection.Add(new MultipartFormDataSection("参数1", 参数2));
    /// </summary>
    private List<IMultipartFormSection> m_FormSection = new List<IMultipartFormSection>();
    /// <summary>
    /// Get请求传参
    /// </summary>
    /// <param name="_url">地址</param>
    /// <param name="_type">参数类型</param>
    /// <param name="_downLoadBegin">开始下载</param>
    /// <param name="_downLoadFinish">下载完成</param>
    /// <param name="_downLoadError">错误回调</param>
    public UnityWebRequestTask(string _url, DownLoadType _type, Action<UnityWebRequest> _downLoadFinish, Action<UnityWebRequest> _downLoadBegin = null, Action<UnityWebRequest, UnityWebRequestTask> _downLoadError = null)
    {
        m_Url = _url;
        m_DownLoadType = _type;
        if (_downLoadBegin != null)
            m_DownLoadBegin = _downLoadBegin;
        if (_downLoadError != null)
            m_DownLoadError = _downLoadError;
        m_DownLoadFinish = _downLoadFinish;
    }
    /// <summary>
    /// Post请求传参
    /// </summary>
    /// <param name="_url">地址</param>
    /// <param name="_type">参数类型</param>
    /// <param name="_downLoadBegin">开始下载</param>
    /// <param name="_downLoadFinish">下载完成</param>
    /// <param name="_downLoadError">错误回调</param>
    /// <param name="_form">form参数</param>
    public UnityWebRequestTask(string _url, DownLoadType _type, WWWForm _form, Action<UnityWebRequest> _downLoadFinish,Action<UnityWebRequest> _downLoadBegin = null , Action<UnityWebRequest, UnityWebRequestTask> _downLoadError = null )
    {
        m_Url = _url;
        m_DownLoadType = _type;
        m_WWWForm = _form;
        m_DownLoadFinish = _downLoadFinish;
        if (_downLoadBegin!=null)
             m_DownLoadBegin = _downLoadBegin;
        if (_downLoadError!=null)
             m_DownLoadError = _downLoadError;
    }
    /// <summary>
    /// Post请求传参
    /// </summary>
    /// <param name="_url">地址</param>
    /// <param name="_type">参数类型</param>
    /// <param name="_downLoadBegin">开始下载</param>
    /// <param name="_downLoadFinish">下载完成</param>
    /// <param name="_downLoadError">错误回调</param>
    /// <param name="_section">参数</param>
    public UnityWebRequestTask(string _url, DownLoadType _type, List<IMultipartFormSection> _section, Action<UnityWebRequest> _downLoadFinish, Action<UnityWebRequest> _downLoadBegin = null, Action<UnityWebRequest, UnityWebRequestTask> _downLoadError = null)
    {
        m_Url = _url;
        m_DownLoadType = _type;
        m_FormSection = _section;
        m_DownLoadFinish = _downLoadFinish;
        if (_downLoadBegin != null)
            m_DownLoadBegin = _downLoadBegin;
        if (_downLoadError != null)
            m_DownLoadError = _downLoadError;
    }
    /// <summary>
    /// 开始下载
    /// </summary>
    /// <returns></returns>
    public IEnumerator DownLoad()
    {
        UnityWebRequest webRequest;
        switch (m_DownLoadType)
        {
            case DownLoadType.GET:
                webRequest = UnityWebRequest.Get(m_Url);
                break;
            case DownLoadType.POST:
                webRequest = UnityWebRequest.Post(m_Url, m_WWWForm);
                break;
            case DownLoadType.POSTMULTIPART:
                webRequest = UnityWebRequest.Post(m_Url, m_FormSection);
                break;
            default:
                webRequest = new UnityWebRequest("http://www.baidu.com");
                break;
        }
        //if (m_DownLoadBegin != null)
        //{
        //    m_DownLoadBegin(webRequest);
        //}
        //等同于非空判断
        m_DownLoadBegin?.Invoke(webRequest);
        webRequest.timeout = 30;
        yield return webRequest.SendWebRequest();
        if (!webRequest.isNetworkError)
        {
            m_DownLoadFinish?.Invoke(webRequest);
        }
        else
        {
            m_DownLoadError?.Invoke(webRequest, this);
            Debug.LogError("webRequest Error : " + webRequest.error);
            Debug.LogError("webRequest Error url : " + m_Url);
        }
    }
}

 

 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public enum DownLoadType
{
    NONE,
    GET,
    POST,
    POSTMULTIPART
}

public class UnityWebRequestTask
{
    /// <summary>   
    /// 下载开始  
    /// </summary>   
    private Action<UnityWebRequest> m_DownLoadBegin;

    /// <summary>
    /// 下载结束
    /// </summary>   
    public Action<UnityWebRequest> m_DownLoadFinish;

    /// <summary>
    /// 下载错误
    /// </summary>   
    private Action<UnityWebRequest, UnityWebRequestTask> m_DownLoadError;

    /// <summary>
    /// 下载类型
    /// </summary>  
    private DownLoadType m_DownLoadType;

    /// <summary>
    /// 下载地址
    /// </summary> 
    private string m_Url { get; set; }

    private WWWForm m_WWWForm;

    /// <summary>
    /// 表单参数  和 WWWForm 用法类似   
    /// 使用方式:
    /// m_FormSection.Add(new MultipartFormDataSection("参数1", 参数2));   
    /// </summary>   
    private List<IMultipartFormSection> m_FormSection = new List<IMultipartFormSection>();

    /// <summary>
    /// Get请求传参    
    /// </summary>
    /// <param name="_url">地址</param>    
    /// <param name="_type">参数类型</param>   
    /// <param name="_downLoadBegin">开始下载</param>    
    /// <param name="_downLoadFinish">下载完成</param>   
    /// <param name="_downLoadError">错误回调</param>    
    public UnityWebRequestTask(string _url, DownLoadType _type, Action<UnityWebRequest> _downLoadFinish,
        Action<UnityWebRequest> _downLoadBegin = null,
        Action<UnityWebRequest, UnityWebRequestTask> _downLoadError = null)
    {
        m_Url = _url;
        m_DownLoadType = _type;
        if (_downLoadBegin != null)
            m_DownLoadBegin = _downLoadBegin;
        if (_downLoadError != null)
            m_DownLoadError = _downLoadError;
        m_DownLoadFinish = _downLoadFinish;
    }

    /// <summary>   
    /// Post请求传参   
    /// </summary>   
    /// <param name="_url">地址</param>    
    /// <param name="_type">参数类型</param>   
    /// <param name="_downLoadBegin">开始下载</param>    
    /// <param name="_downLoadFinish">下载完成</param>    
    /// <param name="_downLoadError">错误回调</param>    
    /// <param name="_form">form参数</param>    
    public UnityWebRequestTask(string _url, DownLoadType _type, WWWForm _form, Action<UnityWebRequest> _downLoadFinish,
        Action<UnityWebRequest> _downLoadBegin = null,
        Action<UnityWebRequest, UnityWebRequestTask> _downLoadError = null)
    {
        m_Url = _url;
        m_DownLoadType = _type;
        m_WWWForm = _form;
        m_DownLoadFinish = _downLoadFinish;
        if (_downLoadBegin != null) m_DownLoadBegin = _downLoadBegin;
        if (_downLoadError != null) m_DownLoadError = _downLoadError;
    }

    /// <summary>    
    /// Post请求传参    
    /// </summary>    
    /// <param name="_url">地址</param>
    /// <param name="_type">参数类型</param>
    /// <param name="_downLoadBegin">开始下载</param>
    /// <param name="_downLoadFinish">下载完成</param>
    /// <param name="_downLoadError">错误回调</param>
    /// <param name="_section">参数</param>   
    public UnityWebRequestTask(string _url, DownLoadType _type, List<IMultipartFormSection> _section,
        Action<UnityWebRequest> _downLoadFinish, Action<UnityWebRequest> _downLoadBegin = null,
        Action<UnityWebRequest, UnityWebRequestTask> _downLoadError = null)
    {
        m_Url = _url;
        m_DownLoadType = _type;
        m_FormSection = _section;
        m_DownLoadFinish = _downLoadFinish;
        if (_downLoadBegin != null) m_DownLoadBegin = _downLoadBegin;
        if (_downLoadError != null) m_DownLoadError = _downLoadError;
    }

    /// <summary>    
    /// 开始下载    
    /// </summary>   
    /// <returns></returns>   
    public IEnumerator DownLoad()
    {
        UnityWebRequest webRequest;
        switch (m_DownLoadType)
        {
            case DownLoadType.GET:
                webRequest = UnityWebRequest.Get(m_Url);
                break;
            case DownLoadType.POST:
                webRequest = UnityWebRequest.Post(m_Url, m_WWWForm);
                break;
            case DownLoadType.POSTMULTIPART:
                webRequest = UnityWebRequest.Post(m_Url, m_FormSection);
                break;
            default:
                webRequest = new UnityWebRequest("http://www.baidu.com");
                break;
        }

        //if (m_DownLoadBegin != null)
        //{       
        //    m_DownLoadBegin(webRequest);      
        //}      
        //等同于非空判断       
        m_DownLoadBegin?.Invoke(webRequest);
        webRequest.timeout = 30;
        yield return webRequest.SendWebRequest();
        if (!webRequest.isNetworkError)
        {
            m_DownLoadFinish?.Invoke(webRequest);
        }
        else
        {
            m_DownLoadError?.Invoke(webRequest, this);
            Debug.LogError("webRequest Error : " + webRequest.error);
            Debug.LogError("webRequest Error url : " + m_Url);
        }
    }
}

  

posted @ 2019-07-01 19:41  不够自律的人  阅读(1124)  评论(0编辑  收藏  举报