当前位置:   article > 正文

【c#编程技术总结】UnityWebRequest用法总结测试<一>_unity webrequest action

unity webrequest action

群里有人问UnityWebRequest的用法,因为本人之前用的也不多只用过Post请求获取服务端数据,就想着把相关的用法总结一下,懒得描述具体用法了,懂得人自然懂,哈哈

  1. void Start()
  2. {
  3. //访问网站
  4. WebRequestManager.Instance.Get("http://www.huanshj.com/", (a) => { Debug.LogError(a.isDone); });
  5. //下载服务器视频文件
  6. WebRequestManager.Instance.DownloadFile("http://www.huanshj.com/World/Videos/1.mp4", Application.dataPath + "/HSJExample/UnityWebRequest/aaa.mp4", (a) => { Debug.LogError(a.downloadProgress); });
  7. //下载服务器图片文件
  8. WebRequestManager.Instance.GetTexture("http://www.huanshj.com/Private/Wedding/Pictures/Xu001.jpg", (a) => { rawImage.texture = a; });
  9. }
  1. #region 模块信息
  2. // **********************************************************************
  3. // Copyright (C) 2020
  4. // Please contact me if you have any questions
  5. // File Name: WebRequestManager
  6. // Author: 幻世界
  7. // QQ: 853394528
  8. // **********************************************************************
  9. #endregion
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using UnityEngine;
  15. using UnityEngine.Experimental.PlayerLoop;
  16. using UnityEngine.Networking;
  17. public class WebRequestManager : BaseManager<WebRequestManager>
  18. {
  19. public void Get(string url, Action<UnityWebRequest> actionResult)
  20. {
  21. StartCoroutine(_Get(url, actionResult));
  22. }
  23. public void DownloadFile(string url, string downloadFilePathAndName, Action<UnityWebRequest> actionResult)
  24. {
  25. StartCoroutine(_DownloadFile(url, downloadFilePathAndName, actionResult));
  26. }
  27. public void GetTexture(string url, Action<Texture2D> actionResult)
  28. {
  29. StartCoroutine(_GetTexture(url, actionResult));
  30. }
  31. public void GetAssetBundle(string url, Action<AssetBundle> actionResult)
  32. {
  33. StartCoroutine(_GetAssetBundle(url, actionResult));
  34. }
  35. public void GetAudioClip(string url, Action<AudioClip> actionResult, AudioType audioType = AudioType.WAV)
  36. {
  37. StartCoroutine(_GetAudioClip(url, actionResult, audioType));
  38. }
  39. public void Post(string serverURL, WWWForm wWWForm, Action<UnityWebRequest> actionResult)
  40. {
  41. wWWForm.AddField("test","我是测试内容");
  42. StartCoroutine(_Post(serverURL, wWWForm, actionResult));
  43. }
  44. /// <summary>
  45. /// 通过PUT方式将字节流传到服务器
  46. /// </summary>
  47. /// <param name="contentBytes">需要上传的字节流</param>
  48. /// <param name="resultAction">处理返回结果的委托</param>
  49. /// <returns></returns>
  50. public void UploadByPut(string url, byte[] contentBytes, Action<bool> actionResult)
  51. {
  52. StartCoroutine(_UploadByPut(url, contentBytes, actionResult, ""));
  53. }
  54. /// <summary>
  55. /// GET请求
  56. /// </summary>
  57. /// <param name="action">请求发起后处理回调结果的委托</param>
  58. /// <returns></returns>
  59. IEnumerator _Get(string url, Action<UnityWebRequest> actionResult)
  60. {
  61. using (UnityWebRequest uwr = UnityWebRequest.Get(url))
  62. {
  63. yield return uwr.SendWebRequest();
  64. actionResult?.Invoke(uwr);
  65. }
  66. }
  67. /// 下载文件
  68. UnityWebRequest uwr;
  69. IEnumerator _DownloadFile(string url, string downloadFilePathAndName, Action<UnityWebRequest> actionResult)
  70. {
  71. uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
  72. uwr.downloadHandler = new DownloadHandlerFile(downloadFilePathAndName);
  73. yield return uwr.SendWebRequest();
  74. actionResult?.Invoke(uwr);
  75. }
  76. float a;
  77. protected override void Update()
  78. {
  79. if (uwr!=null)
  80. {
  81. Debug.Log(uwr.downloadProgress);
  82. }
  83. }
  84. /// 请求图片
  85. IEnumerator _GetTexture(string url, Action<Texture2D> actionResult)
  86. {
  87. UnityWebRequest uwr = new UnityWebRequest(url);
  88. DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
  89. uwr.downloadHandler = downloadTexture;
  90. yield return uwr.SendWebRequest();
  91. Texture2D t = null;
  92. if (!(uwr.isNetworkError || uwr.isHttpError))
  93. {
  94. t = downloadTexture.texture;
  95. }
  96. actionResult?.Invoke(t);
  97. }
  98. /// 请求AssetBundle
  99. IEnumerator _GetAssetBundle(string url, Action<AssetBundle> actionResult)
  100. {
  101. UnityWebRequest www = new UnityWebRequest(url);
  102. DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(www.url, uint.MaxValue);
  103. www.downloadHandler = handler;
  104. yield return www.SendWebRequest();
  105. AssetBundle bundle = null;
  106. if (!(www.isNetworkError || www.isHttpError))
  107. {
  108. bundle = handler.assetBundle;
  109. }
  110. actionResult?.Invoke(bundle);
  111. }
  112. //音效
  113. IEnumerator _GetAudioClip(string url, Action<AudioClip> actionResult, AudioType audioType = AudioType.WAV)
  114. {
  115. using (var uwr = UnityWebRequestMultimedia.GetAudioClip(url, audioType))
  116. {
  117. yield return uwr.SendWebRequest();
  118. if (!(uwr.isNetworkError || uwr.isHttpError))
  119. {
  120. actionResult?.Invoke(DownloadHandlerAudioClip.GetContent(uwr));
  121. }
  122. }
  123. }
  124. /// 向服务器提交post请求
  125. IEnumerator _Post(string serverURL, WWWForm wWWForm, Action<UnityWebRequest> actionResult)
  126. {
  127. UnityWebRequest uwr = UnityWebRequest.Post(serverURL, wWWForm);
  128. yield return uwr.SendWebRequest();
  129. actionResult?.Invoke(uwr);
  130. }
  131. /// <summary>
  132. /// 通过PUT方式将字节流传到服务器
  133. /// </summary>
  134. /// <param name="contentBytes">需要上传的字节流</param>
  135. /// <param name="resultAction">处理返回结果的委托</param>
  136. /// <param name="resultAction">设置header文件中的Content-Type属性</param>
  137. /// <returns></returns>
  138. IEnumerator _UploadByPut(string url, byte[] contentBytes, Action<bool> actionResult, string contentType = "application/octet-stream")
  139. {
  140. UnityWebRequest uwr = new UnityWebRequest();
  141. UploadHandler uploader = new UploadHandlerRaw(contentBytes);
  142. uploader.contentType = contentType;
  143. uwr.uploadHandler = uploader;
  144. yield return uwr.SendWebRequest();
  145. bool res = true;
  146. if (uwr.isNetworkError || uwr.isHttpError)
  147. {
  148. res = false;
  149. }
  150. actionResult?.Invoke(res);
  151. }
  152. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/95738
推荐阅读
相关标签
  

闽ICP备14008679号