Unity 简单封装UnityWebRequest

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

public class C_UnityWebRequest : MonoBehaviour
{
    static C_UnityWebRequest instance;
    public static C_UnityWebRequest Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject mounter = new GameObject("C_UnityWebRequest");
                instance = mounter.AddComponent();
            }
            return instance;
        }
    }

    /*
    UnityWebRequest uwr = new UnityWebRequest();
    uwr.url = "http://www.mysite.com";
    uwr.method = UnityWebRequest.kHttpVerbGET;   // can be set to any custom method, common constants privided

    uwr.useHttpContinue = false;
    uwr.chunkedTransfer = false;
    uwr.redirectLimit = 0;  // disable redirects
    uwr.timeout = 60;       // don't make this small, web requests do take some time 
    */

    /// 
    /// GET请求
    /// 
    /// 
    /// 
    public void Get(string url, Action actionResult)
    {
        StartCoroutine(_Get(url, actionResult));
    }

    /// 
    /// 下载文件
    /// 
    /// 请求地址
    /// 储存文件的路径和文件名 like 'Application.persistentDataPath+"/unity3d.html"'
    /// 请求发起后处理回调结果的委托,处理请求对象
    /// 
    public void DownloadFile(string url, string downloadFilePathAndName, Action actionResult)
    {
        StartCoroutine(_DownloadFile(url, downloadFilePathAndName, actionResult));
    }

    /// 
    /// 请求图片
    /// 
    /// 图片地址,like 'http://www.my-server.com/image.png '
    /// 请求发起后处理回调结果的委托,处理请求结果的图片
    /// 
    public void GetTexture(string url, Action actionResult)
    {
        StartCoroutine(_GetTexture(url, actionResult));
    }

    /// 
    /// 请求AssetBundle
    /// 
    /// AssetBundle地址,like 'http://www.my-server.com/myData.unity3d'
    /// 请求发起后处理回调结果的委托,处理请求结果的AssetBundle
    /// 
    public void GetAssetBundle(string url, Action actionResult)
    {
        StartCoroutine(_GetAssetBundle(url, actionResult));
    }

    /// 
    /// 请求服务器地址上的音效
    /// 
    /// 没有音效地址,like 'http://myserver.com/mysound.wav'
    /// 请求发起后处理回调结果的委托,处理请求结果的AudioClip
    /// 音效类型
    /// 
    public void GetAudioClip(string url, Action actionResult, AudioType audioType = AudioType.WAV)
    {
        StartCoroutine(_GetAudioClip(url, actionResult, audioType));
    }

    /// 
    /// 向服务器提交post请求
    /// 
    /// 服务器请求目标地址,like "http://www.my-server.com/myform"
    /// form表单参数
    /// 处理返回结果的委托,处理请求对象
    /// 
    public void Post(string serverURL, List lstformData, Action actionResult)
    {
        //List formData = new List();
        //formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
        //formData.Add(new MultipartFormFileSection("my file data", "myfile.txt"));

        StartCoroutine(_Post(serverURL, lstformData, actionResult));
    }

    /// 
    /// 通过PUT方式将字节流传到服务器
    /// 
    /// 服务器目标地址 like 'http://www.my-server.com/upload' 
    /// 需要上传的字节流
    /// 处理返回结果的委托
    /// 
    public void UploadByPut(string url, byte[] contentBytes, Action actionResult)
    {
        StartCoroutine(_UploadByPut(url, contentBytes, actionResult,""));
    }

    /// 
    /// GET请求
    /// 
    /// 请求地址,like 'http://www.my-server.com/ '
    /// 请求发起后处理回调结果的委托
    /// 
    IEnumerator _Get(string url, Action actionResult)
    {
        using (UnityWebRequest uwr = UnityWebRequest.Get(url))
        {
            yield return uwr.SendWebRequest();
            if (actionResult != null)
            {
                actionResult(uwr);
            }
        }
    }

    /// 
    /// 下载文件
    /// 
    /// 请求地址
    /// 储存文件的路径和文件名 like 'Application.persistentDataPath+"/unity3d.html"'
    /// 请求发起后处理回调结果的委托,处理请求对象
    /// 
    IEnumerator _DownloadFile(string url,string downloadFilePathAndName,Action actionResult)
    {
        var uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
        uwr.downloadHandler = new DownloadHandlerFile(downloadFilePathAndName);
        yield return uwr.SendWebRequest();
        if (actionResult != null)
        {
            actionResult(uwr);
        }
    }

    /// 
    /// 请求图片
    /// 
    /// 图片地址,like 'http://www.my-server.com/image.png '
    /// 请求发起后处理回调结果的委托,处理请求结果的图片
    /// 
    IEnumerator _GetTexture(string url, Action actionResult)
    {
        UnityWebRequest uwr = new UnityWebRequest(url);
        DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
        uwr.downloadHandler = downloadTexture;
        yield return uwr.SendWebRequest();
        Texture2D t = null;
        if (!(uwr.isNetworkError || uwr.isHttpError))
        {
            t = downloadTexture.texture;
        }
        if (actionResult != null)
        {
            actionResult(t);
        }
    }

    /// 
    /// 请求AssetBundle
    /// 
    /// AssetBundle地址,like 'http://www.my-server.com/myData.unity3d'
    /// 请求发起后处理回调结果的委托,处理请求结果的AssetBundle
    /// 
    IEnumerator _GetAssetBundle(string url,Action actionResult)
    {
        UnityWebRequest www = new UnityWebRequest(url);
        DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(www.url, uint.MaxValue);
        www.downloadHandler = handler;
        yield return www.SendWebRequest();
        AssetBundle bundle = null;
        if (!(www.isNetworkError || www.isHttpError))
        {
            bundle = handler.assetBundle;
        }
        if (actionResult != null)
        {
            actionResult(bundle);
        }
    }

    /// 
    /// 请求服务器地址上的音效
    /// 
    /// 没有音效地址,like 'http://myserver.com/mysound.wav'
    /// 请求发起后处理回调结果的委托,处理请求结果的AudioClip
    /// 音效类型
    /// 
    IEnumerator _GetAudioClip(string url,Action actionResult, AudioType audioType = AudioType.WAV)
    {
        using (var uwr = UnityWebRequestMultimedia.GetAudioClip(url, audioType))
        {
            yield return uwr.SendWebRequest();
            if (!(uwr.isNetworkError || uwr.isHttpError))
            {
                if (actionResult != null)
                {
                    actionResult(DownloadHandlerAudioClip.GetContent(uwr));
                }
            }
        }
    }

    /// 
    /// 向服务器提交post请求
    /// 
    /// 服务器请求目标地址,like "http://www.my-server.com/myform"
    /// form表单参数
    /// 处理返回结果的委托
    /// 
    IEnumerator _Post(string serverURL, List lstformData, Action actionResult)
    {
        //List formData = new List();
        //formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
        //formData.Add(new MultipartFormFileSection("my file data", "myfile.txt"));
        UnityWebRequest uwr = UnityWebRequest.Post(serverURL, lstformData);
        yield return uwr.SendWebRequest();
        if (actionResult != null)
        {
            actionResult(uwr);
        }
    }

    /// 
    /// 通过PUT方式将字节流传到服务器
    /// 
    /// 服务器目标地址 like 'http://www.my-server.com/upload' 
    /// 需要上传的字节流
    /// 处理返回结果的委托
    /// 设置header文件中的Content-Type属性
    /// 
    IEnumerator _UploadByPut(string url,byte[] contentBytes, Action actionResult,string contentType= "application/octet-stream")
    {
        UnityWebRequest uwr = new UnityWebRequest();
        UploadHandler uploader = new UploadHandlerRaw(contentBytes);

        // Sends header: "Content-Type: custom/content-type";
        uploader.contentType = contentType;

        uwr.uploadHandler = uploader;

        yield return uwr.SendWebRequest();

        bool res = true;
        if (uwr.isNetworkError || uwr.isHttpError)
        {
            res = false;
        }
        if (actionResult != null)
        {
            actionResult(res); 
        }
    }

}

你可能感兴趣的:(Unity 简单封装UnityWebRequest)