UnityWebRequest使用方法
一:Unity网络工具类:
Unity原来有个Http访问的工具类WWW,在新版本的Unity中,推出了新的工具类UnityWebRequest,可以代替原来的WWW类。我们之后尽量使用UnityWebRequest进行请求即可。
二:简单的Get访问
UnityWebRequest包含多个静态方法,可以创建一个UnityWebRequest对象,调用SendWebRequest正式发送请求。在协程返回结果后,需要判断是否有错误,再做处理。
IEnumerator GetText(){//直接使用Get方法即可UnityWebRequest www = UnityWebRequest.Get("http://127.0.0.1:9997/gameInit?uid=7");//设置请求超时时间www.timeout = 5;//send的时候,该请求才开始执行yield return www.SendWebRequest();if (www.isNetworkError || www.isHttpError){Debug.Log(www.error);}else{// Show results as textDebug.Log(www.downloadHandler.text); // Or retrieve results as binary databyte[] results = www.downloadHandler.data;}}
三:Http post访问
项目中post访问提交的数据都是json格式的,这里需要对提交的数据uploadHandler做处理,把提交的数据抓换为byte数组。
public IEnumerator PostRequest(string url, string jsonString, Action<string> callback){using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")){byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonString);webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();yield return webRequest.SendWebRequest();if (webRequest.isHttpError || webRequest.isNetworkError){Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);if (callback != null){callback(null);}}else{if (callback != null){callback(webRequest.downloadHandler.text);}}}}
外面直接调用即可,本质上就是初始化一个WebRequest,然后设置对应的上传和下载Handler。最后请求完成调用对应的委托即可
四:下载AssetBundle
IEnumerator GetAssetBundle() {
const string url1 = @"http://127.0.0.1:8080/mat";
const string url2 = @"http://127.0.0.1:8080/image";
UnityWebRequest request1 = UnityWebRequestAssetBundle.GetAssetBundle(url1); //Unity网络请求AssetBundle.获取资源(网络地址1)
UnityWebRequest request2 = UnityWebRequestAssetBundle.GetAssetBundle(url2); //传入地址2
yield return request1.SendWebRequest(); //发送Web请求1
yield return request2.SendWebRequest(); //发送web请求2
AssetBundle ab1 = DownloadHandlerAssetBundle.GetContent(request1); //下载资源,获取连接请求1,返回AssetBundle资源
AssetBundle ab2 = DownloadHandlerAssetBundle.GetContent(request2); //获取连接请求2,返回AssetBundle资源
//获取AsseBundle资源,之后可以做任意事情
}
五:接收Texture
可以使用 UnityWebRequestTexture.GetTexture方法,从远程服务端获取一个Texture文件。这个功能和UnityWebRequest.GET方法非常类似,但是它对下载和存储Texture文件做了优化,提高了处理效率。UnityWebRequest.Texture同样需要一个字符串参数,这个参数就是要下载的资源的URL地址。
IEnumerator GetTexture(){//直接封装好下载图片的函数,不需要再讲字节转换为图片。该类内部自动绑定了DownloadHandlerTextureUnityWebRequest www = UnityWebRequestTexture.GetTexture("http://127.0.0.1:9999/BidCardBorder.png");yield return www.SendWebRequest();if (www.isHttpError || www.isNetworkError){Debug.LogError(www.error);}else{Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;this.GetComponent<MeshRenderer>().material.mainTexture = myTexture;}}
六:Post键值对方式请求
有时候,我们在传递参数的时候,使用的是Key-Value的方式,代码如下所示
IEnumerator PostKeyValue(){WWWForm form = new WWWForm();//键值对form.AddField("name", "zhangsan");form.AddField("password", "123456");//请求链接,并将form对象发送到远程服务器UnityWebRequest webRequest = UnityWebRequest.Post("http://127.0.0.1:8080/login", form);yield return webRequest.SendWebRequest();if (webRequest.isHttpError || webRequest.isNetworkError){Debug.Log(webRequest.error);}else{Debug.Log("发送成功");}}
七:普通Get请求两种方式对比
使用系统提供的UnityWebRequest.Get的时候,已经默认帮我们绑定好了DownloadHandler,无需我们自己设置。当我们使用自己的时候,必须进行对应的设置,代码如下:
IEnumerator SendRequest(){Uri uri = new Uri("http://127.0.0.1:9997/gameInit?uid=7");//1:创建UnityWebRequest对象UnityWebRequest uwr = new UnityWebRequest(uri);uwr.method = "GET";uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();//2:创建UnityWebRequest对象,与上面的等价// UnityWebRequest uwr = UnityWebRequest.Get("http://127.0.0.1:9997/gameInit?uid=7"); uwr.timeout = 5;//设置请求超时时间yield return uwr.SendWebRequest(); //等待返回请求的信息if (uwr.isHttpError || uwr.isNetworkError) //如果其 请求失败,或是 网络错误{Debug.LogError(uwr.error); //打印错误原因}else //请求成功{Debug.Log("请求成功");//使用DownloadHandler下载数据Debug.Log(uwr.downloadHandler.text);//解析对应的Json数据即可}}
UnityWebRequest使用方法相关推荐
- Unity2021版本UnityWebRequest使用方法参考
获取AssetBundle: IEnumerator GetAssetBundle(string path, UnityAction<AssetBundle> onGetAssetBund ...
- [专栏精选]UnityWebRequest详解
本文节选自洪流学堂公众号技术专栏<大话Unity2019>,未经允许不可转载. 洪流学堂公众号回复专栏,查看更多专栏文章. 洪流学堂,让你快人几步.你好,我是郑洪智. 大智:"昨 ...
- Unity最新版打包AssetBundle和加载的方法
一.设置assetBundleName 二.构建AssetBundle包 三.上传AssetBundle到服务器 四.把AssetBundle放到本地 五.操作AssetBundle 六.完整例子 七 ...
- unity当前项目里面AssetBundle和加载的方法总结
2017年10月14日 19:24:14 醉忘天涯一线天 阅读数:9021 出处:https://blog.csdn.net/lmyfx/article/details/78236225 版权声明:本 ...
- Unity资源管理——使用UnityWebRequest从云端下载Assetbundle包
1.环境 基于Unity2018.2 2.思路 1.使用UnityWebRequest.Get方法去获取AB包 2.在协程中返回UnityWebRequest实例对象的SendWebRequest方法 ...
- 【专栏精选】Unity中的HTTP网络通信
本文节选自洪流学堂公众号技术专栏<大话Unity2019>,未经允许不可转载. 洪流学堂公众号回复专栏,查看更多专栏文章. 洪流学堂,让你快人几步.你好,我是郑洪智. 大智:"小 ...
- [已解决]Unity使用WebRequest过程中发生内存问题A Native Collection has not been disposed
问题背景 Unity版本:2021.3.16 在用Unity做一个文字识别的应用,方案是用UnityWebRequest把图片发送给web api,但在发送一定时间后Unity会报错. (具体堆栈信息 ...
- 在 Oracle Enterprise Linux 和 iSCSI 上构建您自己的 Oracle RAC 11g 集群
作者:Jeffrey Hunter 了解如何以低于 2,700 美元的费用在 Oracle Enterprise Linux 上安装并配置 Oracle RAC 11g 第 2 版开发集群. 本指南中 ...
- Java面试题大全2021版
一.Java 基础 JDK 和 JRE 有什么区别? JDK:Java Development Kit 的简称,java 开发工具包,提供了 java 的开发环境和运行环境. JRE:Java Run ...
最新文章
- 《深入理解计算机系统》学习心得二:关于show-bytes的 学习
- HTML5 元素拖拽实现 及 jquery.event.drag插件
- mysql优化问题?_MYSQL优化问题
- Winform中实现读取xml配置文件并动态配置DevExpress的RadioGroup的选项
- linux终端帮助,Linux下的帮助命令
- html中引入js柱状图,Echarts.js 图表在layui中的引用
- 北京 | 蚂蚁集团共享智能团队招聘研究实习生
- 程序员私活app排行_iOS程序员,失业就等于成为废人?
- 为Editplus安装smali代码语法高亮插件
- 蓝桥杯 ALGO-23 算法训练 一元三次方程求解
- 网站平台架构演变史(四) - 水平拆分的查询
- 赤兔CMS(CTCMS)V2.0.6版更新了后台模板样式
- java实现远程桌面监控
- mysql 共享锁(读写锁) 修改数据问题(update,insert)(LOCK IN SHARE MODE)
- [自我介绍]第一篇博客
- mybatis替换大于号或者小于号
- STM32:使用外部中断控制对射式红外传感器并计次
- Spring中的 @Lazy注解简析
- mac更新后Git无法使用的问题
- Java破解9X9数独小游戏
热门文章
- mnist转换为3通道的224*244,生成训练train.txt和vaild.txt
- Unity FMOD 音效失真变形
- alginate-Ferrocene|海藻酸钠-二茂铁|二茂铁修饰改性海藻酸钠|海藻酸钠-peg-二茂铁
- python操作APP自动化测试脚本
- vmware ESXI 创建虚拟机报错verification failed (0x1a) security violation
- wireshark过滤http包
- 职位扫盲(CEO,CTO。。。)
- python:一些pytho数学题
- 计算机怎样辅助与美工结合,农村中职电商专业学生学好计算机辅助设计软件的意义及方法探索...
- 中国民族博览杂志社《中国民族博览》编辑部2023年第2期目录