UnityWebRequest_ZT
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<C_UnityWebRequest>();
}
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);
}
}
}
ZT:https://www.jianshu.com/p/3da9191f82a0
UnityWebRequest_ZT的更多相关文章
随机推荐
- Android 禁止Edittext弹出系统软键盘 的几种方法
第一种方法:在XML文件下添加: android:focusable="true" android:focusableInTouchMode="true" 第二 ...
- Mycat 关键特性
关键特性 支持SQL92标准 支持MySQL.Oracle.DB2.SQL Server.PostgreSQL等DB的常见SQL语法 遵守Mysql原生协议,跨语言,跨平台,跨数据库的通用中间件代理. ...
- 利用threading模块开线程
一多线程的概念介绍 threading模块介绍 threading模块和multiprocessing模块在使用层面,有很大的相似性. 二.开启多线程的两种方式 1.创建线程的开销比创建进程的开销小, ...
- 下载git2.2.1并将git添加到环境变量中
># wget https://github.com/git/git/archive/v2.2.1.tar.gz > # tar zxvf v2.2.1.tar.gz ># cd g ...
- RedHat安装git报错 expected specifier-qualifier-list before ‘z_stream’
年初开学的时候认识到了git,因为当时也没装虚拟机甚至是不知道虚拟机这个东西,所以就下载了Windows下的git.当时跟着廖雪峰Git教程 学了几个命令.安装了虚拟机,也学了linux的基本命令后, ...
- ELK 学习笔记之 Logstash之filter配置
Logstash之filter: json filter: input{ stdin{ } } filter{ json{ source => "message" } } o ...
- 品Spring:关于@Scheduled定时任务的思考与探索,结果尴尬了
非Spring风格的代码与Spring的结合 现在的开发都是基于Spring的,所有的依赖都有Spring管理,这没有问题. 但是要突然写一些非Spring风格的代码时,可能会很不习惯,如果还要和Sp ...
- Android资源管理利器Resources和AssetManager
前言 : Android工程在运行的时候往往需要引用资源.使用 Resources 来获取 res 目录下的各种与设备相关的资源.而使用 AssetManager 来获取 assets 目录下的资源 ...
- Github | 吴恩达新书《Machine Learning Yearning》完整中文版开源
最近开源了周志华老师的西瓜书<机器学习>纯手推笔记: 博士笔记 | 周志华<机器学习>手推笔记第一章思维导图 [博士笔记 | 周志华<机器学习>手推笔记第二章&qu ...
- 清除SQL Server Management Studio的最近服务器列表
C:\Users\dell\AppData\Roaming\Microsoft\SQL Server Management Studio\12.0\SqlStudio.bin