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;
}
}
/// <summary>
/// get请求
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
public void Get(string url, Action<UnityWebRequest> actionResult)
{
StartCoroutine(\_Get(url, actionResult));
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="url"></param>
/// <param name="downloadFilePathAndName"></param>
/// <param name="actionResult"></param>
public void DownloadFile(string url, string downloadFilePathAndName, Action<UnityWebRequest> actionResult)
{
StartCoroutine(\_DownloadFile(url, downloadFilePathAndName, actionResult));
}
/// <summary>
/// 请求图片
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
public void GetTexture(string url, Action<Texture2D> actionResult)
{
StartCoroutine(\_GetTexture(url, actionResult));
}
/// <summary>
/// 请求assetBuddle
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
public void GetAssetBundle(string url, Action<AssetBundle> actionResult)
{
StartCoroutine(\_GetAssetBundle(url, actionResult));
}
/// <summary>
/// 下载音效
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <param name="audioType"></param>
public void GetAudioClip(string url, Action<AudioClip> actionResult, AudioType audioType = AudioType.WAV)
{
StartCoroutine(\_GetAudioClip(url, actionResult, audioType));
}
/// <summary>
/// post form
/// </summary>
/// <param name="serverURL"></param>
/// <param name="lstformData"></param>
/// <param name="actionResult"></param>
public void Post(string serverURL, List<IMultipartFormSection> lstformData, Action<UnityWebRequest> actionResult)
{
//List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
//formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
//formData.Add(new MultipartFormFileSection("my file data", "myfile.txt"));
StartCoroutine(\_Post(serverURL, lstformData, actionResult));
}
/// <summary>
/// put方法
/// </summary>
/// <param name="url"></param>
/// <param name="contentBytes"></param>
/// <param name="actionResult"></param>
public void UploadByPut(string url, byte\[\] contentBytes, Action<bool> actionResult)
{
StartCoroutine(\_UploadByPut(url, contentBytes, actionResult, ""));
}
/// <summary>
/// get
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator \_Get(string url, Action<UnityWebRequest> actionResult)
{
using (UnityWebRequest uwr = UnityWebRequest.Get(url))
{
yield return uwr.SendWebRequest();
if (actionResult != null)
{
actionResult(uwr);
}
}
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="url"></param>
/// <param name="downloadFilePathAndName"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator \_DownloadFile(string url, string downloadFilePathAndName, Action<UnityWebRequest> actionResult)
{
var uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
uwr.downloadHandler = new DownloadHandlerFile(downloadFilePathAndName);
yield return uwr.SendWebRequest();
if (actionResult != null)
{
actionResult(uwr);
}
}
/// <summary>
/// 下载图片
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator \_GetTexture(string url, Action<Texture2D> 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);
}
}
/// <summary>
/// 下载AssetBuddle
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator \_GetAssetBundle(string url, Action<AssetBundle> 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);
}
}
/// <summary>
/// 下载音效
/// </summary>
/// <param name="url"></param>
/// <param name="actionResult"></param>
/// <param name="audioType"></param>
/// <returns></returns>
IEnumerator \_GetAudioClip(string url, Action<AudioClip> 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));
}
}
}
}
/// <summary>
/// 表单下载
/// </summary>
/// <param name="serverURL"></param>
/// <param name="lstformData"></param>
/// <param name="actionResult"></param>
/// <returns></returns>
IEnumerator \_Post(string serverURL, List<IMultipartFormSection> lstformData, Action<UnityWebRequest> actionResult)
{
//List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
//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);
}
}
/// <summary>
/// put
/// </summary>
/// <param name="url"></param>
/// <param name="contentBytes"></param>
/// <param name="actionResult"></param>
/// <param name="contentType"></param>
/// <returns></returns>
IEnumerator \_UploadByPut(string url, byte\[\] contentBytes, Action<bool> 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);
}
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章