当前位置:   article > 正文

Unity 简单封装UnityWebRequest_unity unitywebrequest加载预制体

unity unitywebrequest加载预制体

转自 https://www.jianshu.com/p/3da9191f82a0 

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. public class C_UnityWebRequest : MonoBehaviour
  7. {
  8. static C_UnityWebRequest instance;
  9. public static C_UnityWebRequest Instance
  10. {
  11. get
  12. {
  13. if (instance == null)
  14. {
  15. GameObject mounter = new GameObject("C_UnityWebRequest");
  16. instance = mounter.AddComponent<C_UnityWebRequest>();
  17. }
  18. return instance;
  19. }
  20. }
  21. /*
  22. UnityWebRequest uwr = new UnityWebRequest();
  23. uwr.url = "http://www.mysite.com";
  24. uwr.method = UnityWebRequest.kHttpVerbGET; // can be set to any custom method, common constants privided
  25. uwr.useHttpContinue = false;
  26. uwr.chunkedTransfer = false;
  27. uwr.redirectLimit = 0; // disable redirects
  28. uwr.timeout = 60; // don't make this small, web requests do take some time
  29. */
  30. /// <summary>
  31. /// GET请求
  32. /// </summary>
  33. /// <param name="url"></param>
  34. /// <param name="action"></param>
  35. public void Get(string url, Action<UnityWebRequest> actionResult)
  36. {
  37. StartCoroutine(_Get(url, actionResult));
  38. }
  39. /// <summary>
  40. /// 下载文件
  41. /// </summary>
  42. /// <param name="url">请求地址</param>
  43. /// <param name="downloadFilePathAndName">储存文件的路径和文件名 like 'Application.persistentDataPath+"/unity3d.html"'</param>
  44. /// <param name="actionResult">请求发起后处理回调结果的委托,处理请求对象</param>
  45. /// <returns></returns>
  46. public void DownloadFile(string url, string downloadFilePathAndName, Action<UnityWebRequest> actionResult)
  47. {
  48. StartCoroutine(_DownloadFile(url, downloadFilePathAndName, actionResult));
  49. }
  50. /// <summary>
  51. /// 请求图片
  52. /// </summary>
  53. /// <param name="url">图片地址,like 'http://www.my-server.com/image.png '</param>
  54. /// <param name="action">请求发起后处理回调结果的委托,处理请求结果的图片</param>
  55. /// <returns></returns>
  56. public void GetTexture(string url, Action<Texture2D> actionResult)
  57. {
  58. StartCoroutine(_GetTexture(url, actionResult));
  59. }
  60. /// <summary>
  61. /// 请求AssetBundle
  62. /// </summary>
  63. /// <param name="url">AssetBundle地址,like 'http://www.my-server.com/myData.unity3d'</param>
  64. /// <param name="actionResult">请求发起后处理回调结果的委托,处理请求结果的AssetBundle</param>
  65. /// <returns></returns>
  66. public void GetAssetBundle(string url, Action<AssetBundle> actionResult)
  67. {
  68. StartCoroutine(_GetAssetBundle(url, actionResult));
  69. }
  70. /// <summary>
  71. /// 请求服务器地址上的音效
  72. /// </summary>
  73. /// <param name="url">没有音效地址,like 'http://myserver.com/mysound.wav'</param>
  74. /// <param name="actionResult">请求发起后处理回调结果的委托,处理请求结果的AudioClip</param>
  75. /// <param name="audioType">音效类型</param>
  76. /// <returns></returns>
  77. public void GetAudioClip(string url, Action<AudioClip> actionResult, AudioType audioType = AudioType.WAV)
  78. {
  79. StartCoroutine(_GetAudioClip(url, actionResult, audioType));
  80. }
  81. /// <summary>
  82. /// 向服务器提交post请求
  83. /// </summary>
  84. /// <param name="serverURL">服务器请求目标地址,like "http://www.my-server.com/myform"</param>
  85. /// <param name="lstformData">form表单参数</param>
  86. /// <param name="lstformData">处理返回结果的委托,处理请求对象</param>
  87. /// <returns></returns>
  88. public void Post(string serverURL, List<IMultipartFormSection> lstformData, Action<UnityWebRequest> actionResult)
  89. {
  90. //List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
  91. //formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
  92. //formData.Add(new MultipartFormFileSection("my file data", "myfile.txt"));
  93. StartCoroutine(_Post(serverURL, lstformData, actionResult));
  94. }
  95. /// <summary>
  96. /// 通过PUT方式将字节流传到服务器
  97. /// </summary>
  98. /// <param name="url">服务器目标地址 like 'http://www.my-server.com/upload' </param>
  99. /// <param name="contentBytes">需要上传的字节流</param>
  100. /// <param name="resultAction">处理返回结果的委托</param>
  101. /// <returns></returns>
  102. public void UploadByPut(string url, byte[] contentBytes, Action<bool> actionResult)
  103. {
  104. StartCoroutine(_UploadByPut(url, contentBytes, actionResult,""));
  105. }
  106. /// <summary>
  107. /// GET请求
  108. /// </summary>
  109. /// <param name="url">请求地址,like 'http://www.my-server.com/ '</param>
  110. /// <param name="action">请求发起后处理回调结果的委托</param>
  111. /// <returns></returns>
  112. IEnumerator _Get(string url, Action<UnityWebRequest> actionResult)
  113. {
  114. using (UnityWebRequest uwr = UnityWebRequest.Get(url))
  115. {
  116. yield return uwr.SendWebRequest();
  117. if (actionResult != null)
  118. {
  119. actionResult(uwr);
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// 下载文件
  125. /// </summary>
  126. /// <param name="url">请求地址</param>
  127. /// <param name="downloadFilePathAndName">储存文件的路径和文件名 like 'Application.persistentDataPath+"/unity3d.html"'</param>
  128. /// <param name="actionResult">请求发起后处理回调结果的委托,处理请求对象</param>
  129. /// <returns></returns>
  130. IEnumerator _DownloadFile(string url,string downloadFilePathAndName,Action<UnityWebRequest> actionResult)
  131. {
  132. var uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
  133. uwr.downloadHandler = new DownloadHandlerFile(downloadFilePathAndName);
  134. yield return uwr.SendWebRequest();
  135. if (actionResult != null)
  136. {
  137. actionResult(uwr);
  138. }
  139. }
  140. /// <summary>
  141. /// 请求图片
  142. /// </summary>
  143. /// <param name="url">图片地址,like 'http://www.my-server.com/image.png '</param>
  144. /// <param name="action">请求发起后处理回调结果的委托,处理请求结果的图片</param>
  145. /// <returns></returns>
  146. IEnumerator _GetTexture(string url, Action<Texture2D> actionResult)
  147. {
  148. UnityWebRequest uwr = new UnityWebRequest(url);
  149. DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
  150. uwr.downloadHandler = downloadTexture;
  151. yield return uwr.SendWebRequest();
  152. Texture2D t = null;
  153. if (!(uwr.isNetworkError || uwr.isHttpError))
  154. {
  155. t = downloadTexture.texture;
  156. }
  157. if (actionResult != null)
  158. {
  159. actionResult(t);
  160. }
  161. }
  162. /// <summary>
  163. /// 请求AssetBundle
  164. /// </summary>
  165. /// <param name="url">AssetBundle地址,like 'http://www.my-server.com/myData.unity3d'</param>
  166. /// <param name="actionResult">请求发起后处理回调结果的委托,处理请求结果的AssetBundle</param>
  167. /// <returns></returns>
  168. IEnumerator _GetAssetBundle(string url,Action<AssetBundle> actionResult)
  169. {
  170. UnityWebRequest www = new UnityWebRequest(url);
  171. DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(www.url, uint.MaxValue);
  172. www.downloadHandler = handler;
  173. yield return www.SendWebRequest();
  174. AssetBundle bundle = null;
  175. if (!(www.isNetworkError || www.isHttpError))
  176. {
  177. bundle = handler.assetBundle;
  178. }
  179. if (actionResult != null)
  180. {
  181. actionResult(bundle);
  182. }
  183. }
  184. /// <summary>
  185. /// 请求服务器地址上的音效
  186. /// </summary>
  187. /// <param name="url">没有音效地址,like 'http://myserver.com/mysound.wav'</param>
  188. /// <param name="actionResult">请求发起后处理回调结果的委托,处理请求结果的AudioClip</param>
  189. /// <param name="audioType">音效类型</param>
  190. /// <returns></returns>
  191. IEnumerator _GetAudioClip(string url,Action<AudioClip> actionResult, AudioType audioType = AudioType.WAV)
  192. {
  193. using (var uwr = UnityWebRequestMultimedia.GetAudioClip(url, audioType))
  194. {
  195. yield return uwr.SendWebRequest();
  196. if (!(uwr.isNetworkError || uwr.isHttpError))
  197. {
  198. if (actionResult != null)
  199. {
  200. actionResult(DownloadHandlerAudioClip.GetContent(uwr));
  201. }
  202. }
  203. }
  204. }
  205. /// <summary>
  206. /// 向服务器提交post请求
  207. /// </summary>
  208. /// <param name="serverURL">服务器请求目标地址,like "http://www.my-server.com/myform"</param>
  209. /// <param name="lstformData">form表单参数</param>
  210. /// <param name="lstformData">处理返回结果的委托</param>
  211. /// <returns></returns>
  212. IEnumerator _Post(string serverURL, List<IMultipartFormSection> lstformData, Action<UnityWebRequest> actionResult)
  213. {
  214. //List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
  215. //formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
  216. //formData.Add(new MultipartFormFileSection("my file data", "myfile.txt"));
  217. UnityWebRequest uwr = UnityWebRequest.Post(serverURL, lstformData);
  218. yield return uwr.SendWebRequest();
  219. if (actionResult != null)
  220. {
  221. actionResult(uwr);
  222. }
  223. }
  224. /// <summary>
  225. /// 通过PUT方式将字节流传到服务器
  226. /// </summary>
  227. /// <param name="url">服务器目标地址 like 'http://www.my-server.com/upload' </param>
  228. /// <param name="contentBytes">需要上传的字节流</param>
  229. /// <param name="resultAction">处理返回结果的委托</param>
  230. /// <param name="resultAction">设置header文件中的Content-Type属性</param>
  231. /// <returns></returns>
  232. IEnumerator _UploadByPut(string url,byte[] contentBytes, Action<bool> actionResult,string contentType= "application/octet-stream")
  233. {
  234. UnityWebRequest uwr = new UnityWebRequest();
  235. UploadHandler uploader = new UploadHandlerRaw(contentBytes);
  236. // Sends header: "Content-Type: custom/content-type";
  237. uploader.contentType = contentType;
  238. uwr.uploadHandler = uploader;
  239. yield return uwr.SendWebRequest();
  240. bool res = true;
  241. if (uwr.isNetworkError || uwr.isHttpError)
  242. {
  243. res = false;
  244. }
  245. if (actionResult != null)
  246. {
  247. actionResult(res);
  248. }
  249. }
  250. }
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号