封装UnityWebRequest

未完待续.....

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;

public class QWebRequest : MonoBehaviour
{
    private static QWebRequest instance;
    private static QWebRequest Instance
    {
        get
        {
            return instance ?? (instance = new GameObject("QWebRequest").AddComponent());
        }
    }

    #region  startDownload
    /// 
    /// 下载文本
    /// 
    /// 请求的链接
    /// 发生的事件
    /// 请求的方式
    /// 
    public static bool DownloadText(string url, RequestTextEvent action, string method = UnityWebRequest.kHttpVerbGET)
    {
        if (IsNull(url, action, action.action)) return true;
        Instance.StartCoroutine(Instance.DownloadTextIEnumerator( url,method, action));
        return false;
    }

    /// 
    /// 下载数据
    /// 
    /// 请求的链接
    /// 发生的事件
    /// 请求的方式
    /// 
    public static bool DownloadData(string url, RequestDataEvent action, string method = UnityWebRequest.kHttpVerbGET)
    {
        if (IsNull(url, action, action.action)) return true;
        Instance.StartCoroutine(Instance.DownloadDataIEnumerator(url,method,action));
        return false;
    }

    /// 
    /// 下载文件[文件存放位置不能重复]
    /// 
    /// 请求的链接
    /// 保存的路径
    /// 发生的事件
    /// 请求的方式
    /// 
    public static bool DownloadFile(string url, string savePath, RequestFileEvent action, string method = UnityWebRequest.kHttpVerbGET)
    {
        if(string.IsNullOrEmpty(url) || action==null)return true;
        Instance.StartCoroutine(Instance.DownloadFileIEnumerator(url,savePath,method,action));
        return false;
    }

    /// 
    /// 下载图片
    /// 
    /// 请求的链接
    /// 发生的事件
    /// 请求的方式
    /// 
    public static bool DownaloadSprite(string url, RequestSpriteEvent action, string method = UnityWebRequest.kHttpVerbGET)
    {
        if (IsNull(url, action, action.action)) return true;
        Instance.StartCoroutine(Instance.DownloadSpriteIEnumerator(url,method,action));
        return false;
    }

    /// 
    /// 下载AssetBundle
    /// 
    /// 请求的链接
    /// 发生的事件
    /// 请求的方式
    /// 
    public static bool DownaloadAssetBundle(string url, RequestAssetBundleEvent action, string method = UnityWebRequest.kHttpVerbGET)
    {
        if (IsNull(url, action, action.action)) return true;
        Instance.StartCoroutine(Instance.DownloadAssetBundleIEnumerator(url,method,action));
        return false;
    }

    /// 
    /// 下载音频
    /// 
    /// 请求的链接
    /// 音频格式
    /// 发生的事件
    /// 
    public static bool DownloadAudioClip(string url, RequestAudioClipEvent action, AudioType audioType=AudioType.AUDIOQUEUE)
    {
        if (IsNull(url, action, action.action)) return true;
        Instance.StartCoroutine(Instance.DownloadAudioClipIEnumerator(url,audioType,action));
        return false;
    }

    /// 
    /// 下载视频
    /// 
    /// 请求的链接
    /// 发生的事件
    /// 
    public static bool DownloadMovieTexture(string url, RequestMovieTextureEvent action)
    {
        if (IsNull(url, action, action.action)) return true;
        Instance.StartCoroutine(Instance.DownloadMovieTextureIEnumerator(url,action));
        return false;
    }
    #endregion

    IEnumerator DownloadTextIEnumerator(string url, string method, RequestTextEvent action)
    {
        var request = new UnityWebRequest(url, method);
        request.downloadHandler = new DownloadHandlerBuffer();
        if(action.downloadProgress!=null) StartCoroutine(DownloadProgress(request, action.downloadProgress));
        yield return request.SendWebRequest();
        Dispose(request,() =>
        {
            action.action(request.downloadHandler.text);
        }, action.error, action.error404, action.error500);
    }

    IEnumerator DownloadDataIEnumerator( string url, string method, RequestDataEvent action)
    {
        var request = new UnityWebRequest(url, method);
        request.downloadHandler = new DownloadHandlerBuffer();
        if(action.downloadProgress!=null) StartCoroutine(DownloadProgress(request, action.downloadProgress));
        yield return request.SendWebRequest();
        Dispose(request,() =>
        {
            action.action(request.downloadHandler.data);
        }, action.error, action.error404, action.error500);
    }

    IEnumerator DownloadFileIEnumerator(string url, string savePath, string method, RequestFileEvent action)
    {
        if (!File.Exists(savePath))
        {
            var request = new UnityWebRequest(url, method);
            request.downloadHandler = new DownloadHandlerFile(savePath);
            if (action.downloadProgress != null) StartCoroutine(DownloadProgress(request, action.downloadProgress));
            yield return request.SendWebRequest();
            Dispose(request, () =>
             {
                 Debug.Log("File successfully downloaded and saved to " + savePath);
                 if (action != null) action.action();
             }, action.error, action.error404, action.error500);
        }else{
            action.failedCreateFile();
        }
    }

    IEnumerator DownloadSpriteIEnumerator(string url, string method, RequestSpriteEvent action)
    {
        var request = new UnityWebRequest(url, method);
        var texDl = new DownloadHandlerTexture(true);
        request.downloadHandler = texDl;
        if(action.downloadProgress!=null) StartCoroutine(DownloadProgress(request, action.downloadProgress));

        yield return request.SendWebRequest();
        Dispose(request,() =>
        {
            Texture2D t = texDl.texture;
            action.action(Sprite.Create(t, new Rect(0, 0, t.width, t.height), Vector2.zero, 1f),texDl.data);
        }, action.error, action.error404, action.error500);
    }

    IEnumerator DownloadAssetBundleIEnumerator(string url, string method, RequestAssetBundleEvent action)
    {
        var request = new UnityWebRequest(url, method);
        var handler = new DownloadHandlerAssetBundle(request.url, uint.MaxValue);
        request.downloadHandler = handler;
        if(action.downloadProgress!=null) StartCoroutine(DownloadProgress(request, action.downloadProgress));

        yield return request.SendWebRequest();
        Dispose(request,() =>
        {
            action.action(handler.assetBundle);
        }, action.error, action.error404, action.error500);
    }

    IEnumerator DownloadAudioClipIEnumerator(string url, AudioType audioType, RequestAudioClipEvent action)
    {
        var request = UnityWebRequestMultimedia.GetAudioClip(url, audioType);
        if(action.downloadProgress!=null) StartCoroutine(DownloadProgress(request, action.downloadProgress));

        yield return request.SendWebRequest();

        Dispose(request,() =>
        {
            action.action(DownloadHandlerAudioClip.GetContent(request));
        }, action.error, action.error404, action.error500);
    }


    IEnumerator DownloadMovieTextureIEnumerator(string url, RequestMovieTextureEvent action)
    {
        var request = UnityWebRequestMultimedia.GetMovieTexture(url);
        if(action.downloadProgress!=null) StartCoroutine(DownloadProgress(request, action.downloadProgress));
        yield return request.SendWebRequest();
        Dispose(request,() =>
        {
            action.action(DownloadHandlerMovieTexture.GetContent(request));
        }, action.error, action.error404, action.error500);
    }

    IEnumerator DownloadProgress(UnityWebRequest request, Action action)
    {
        while (!request.isDone)
        {

            yield return null;
            action(request.downloadProgress);

        }
        request.Abort();
        Resources.UnloadUnusedAssets();
    }

    private void Dispose(UnityWebRequest request,Action ok, Action error, Action error404, Action error500)
    {
        QDebug.Log(string.Format("url [ {0} ]\nerror [ {1} ]",request.url,request.error));
        if (request.isHttpError || request.isNetworkError)
        {
            if (error != null) error(request.error);
        }
        else
        {
            switch (request.responseCode)
            {
                case 200: if (ok != null) ok(); break;
                case 404: if (error404 != null) error404(); break;
                case 500: if (error500 != null) error500(); break;
            }
        }
    }

    private static bool IsNull(string url, BaseRequestEvent requestAction, object action)
    {
        if (string.IsNullOrEmpty(url) || requestAction == null || action == null)
        {
            QDebug.Log("Can't be empty");
            return true;
        }
        return false;
    }

    public class BaseRequestEvent
    {
        public Action downloadProgress;
        public Action error;
        public Action error404;
        public Action error500;
    }

    /// 
    /// 文本请求事件
    /// 
    public class RequestTextEvent : BaseRequestEvent
    {
        public Action action;
    }

    /// 
    /// 数据请求事件
    /// 
    public class RequestDataEvent : BaseRequestEvent
    {
        public Action action;
    }

    /// 
    /// 文件请求事件
    /// 
    public class RequestFileEvent:BaseRequestEvent
    {
        public Action action;
        public Action failedCreateFile;
    }

    /// 
    /// 图片请求事件
    /// 
    public class RequestSpriteEvent : BaseRequestEvent
    {
        public Action action;
    }

    /// 
    /// 文本请求事件
    /// 
    public class RequestAssetBundleEvent : BaseRequestEvent
    {
        public Action action;
    }

    /// 
    /// 音频请求事件
    /// 
    public class RequestAudioClipEvent : BaseRequestEvent
    {
        public Action action;
    }

    /// 
    /// 视频请求事件
    /// 
    public class RequestMovieTextureEvent : BaseRequestEvent
    {
        public Action action;
    }


}

你可能感兴趣的:(unity3d)