WebRequest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. namespace WorldComposer
  8. {
  9. public class WebRequest
  10. {
  11. const float DEFAULT_DELAY_REQUEST = 0.1f;
  12. const float PAUSE_DELAY_REQUEST = 1.1f;
  13. static List<WebRequest> requests = new List<WebRequest>();
  14. static float tStamp;
  15. static float delayRequest = DEFAULT_DELAY_REQUEST;
  16. #if UNITY_5
  17. public WWW www;
  18. #else
  19. public UnityWebRequest www;
  20. #endif
  21. string url;
  22. bool isTextureRequest;
  23. bool isAddedToList;
  24. int redoCount = 0;
  25. public static void ResetStampTime()
  26. {
  27. tStamp = -1000;
  28. }
  29. public static void PauseRequests()
  30. {
  31. delayRequest = PAUSE_DELAY_REQUEST;
  32. }
  33. static public void ProcessRequests()
  34. {
  35. if (requests.Count == 0) return;
  36. if (Time.realtimeSinceStartup - tStamp < delayRequest) return;
  37. tStamp = Time.realtimeSinceStartup;
  38. if (delayRequest > DEFAULT_DELAY_REQUEST + 0.01f)
  39. {
  40. Debug.Log("Resuming requests.");
  41. delayRequest = DEFAULT_DELAY_REQUEST;
  42. }
  43. try
  44. {
  45. SendWebRequest(requests[0]);
  46. }
  47. catch (Exception e)
  48. {
  49. Debug.LogError(e);
  50. }
  51. requests[0].isAddedToList = false;
  52. requests.RemoveAt(0);
  53. }
  54. static public void SendWebRequest(WebRequest webRequest)
  55. {
  56. #if UNITY_5
  57. webRequest.www = new WWW(webRequest.url);
  58. #else
  59. #if UNITY_2017_1
  60. webRequest.www.Send();
  61. #else
  62. webRequest.www.SendWebRequest();
  63. #endif
  64. #endif
  65. }
  66. public void Request(string url, bool isTextureRequest)
  67. {
  68. url = url.ToString(CultureInfo.InvariantCulture);
  69. if (www != null)
  70. {
  71. if (isAddedToList)
  72. {
  73. requests.Remove(this);
  74. isAddedToList = false;
  75. }
  76. www.Dispose();
  77. www = null;
  78. }
  79. this.url = url;
  80. this.isTextureRequest = isTextureRequest;
  81. #if !UNITY_5
  82. if (isTextureRequest)
  83. {
  84. www = UnityWebRequestTexture.GetTexture(url);
  85. }
  86. else www = UnityWebRequest.Get(url);
  87. #endif
  88. if (Time.realtimeSinceStartup - tStamp < delayRequest)
  89. {
  90. isAddedToList = true;
  91. requests.Add(this);
  92. return;
  93. }
  94. tStamp = Time.realtimeSinceStartup;
  95. SendWebRequest(this);
  96. }
  97. public bool HasRequested
  98. {
  99. get
  100. {
  101. return www != null;
  102. }
  103. }
  104. public bool IsDone
  105. {
  106. get
  107. {
  108. if (www == null) return false;
  109. return www.isDone;
  110. }
  111. }
  112. public bool IsDoneIfErrorRedo
  113. {
  114. get
  115. {
  116. if (www != null && www.isDone)
  117. {
  118. #if UNITY_5
  119. if (!string.IsNullOrEmpty(www.error))
  120. #elif UNITY_2020_1_OR_NEWER
  121. if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.DataProcessingError || www.result == UnityWebRequest.Result.ProtocolError)
  122. #else
  123. if (www.isNetworkError || www.isHttpError)
  124. #endif
  125. {
  126. if (redoCount++ <= 3)
  127. {
  128. if (redoCount == 4)
  129. {
  130. Debug.LogError("Check if your Bing key is corrent, see the WorldComposer window how to solve it.");
  131. }
  132. #if UNITY_2020_1_OR_NEWER
  133. else Debug.LogError(www.error + " response code " + www.responseCode + " www.result " + www.result);
  134. #else
  135. else Debug.LogError(www.error + " response code " + www.responseCode + " isNetworkError " + www.isNetworkError + " isHttpError " + www.isHttpError);
  136. #endif
  137. RedoRequest();
  138. }
  139. return false;
  140. }
  141. return true;
  142. }
  143. return false;
  144. }
  145. }
  146. public bool IsError(out string text)
  147. {
  148. #if UNITY_5
  149. bool isError = !string.IsNullOrEmpty(www.error);
  150. #elif UNITY_2020_1_OR_NEWER
  151. bool isError = www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.DataProcessingError || www.result == UnityWebRequest.Result.ProtocolError;
  152. #else
  153. bool isError = www.isNetworkError || www.isHttpError;
  154. #endif
  155. if (isError)
  156. {
  157. text = www.error;
  158. www = null;
  159. }
  160. else text = string.Empty;
  161. return isError;
  162. }
  163. public void RedoRequest()
  164. {
  165. Request(url, isTextureRequest);
  166. }
  167. public void Abort()
  168. {
  169. if (www != null && !www.isDone)
  170. {
  171. #if UNITY_5
  172. www.Dispose();
  173. #else
  174. www.Abort();
  175. #endif
  176. www = null;
  177. }
  178. }
  179. public string GetText()
  180. {
  181. redoCount = 0;
  182. if (www == null)
  183. {
  184. Debug.LogError("www = null");
  185. return string.Empty;
  186. }
  187. #if UNITY_5
  188. string text = www.text;
  189. #else
  190. if (www.downloadHandler == null)
  191. {
  192. Debug.LogError("downloadHandeler = null");
  193. return string.Empty;
  194. }
  195. string text = www.downloadHandler.text;
  196. #endif
  197. www = null;
  198. return text;
  199. }
  200. public Texture2D GetTexture()
  201. {
  202. redoCount = 0;
  203. #if UNITY_5
  204. Texture2D texture = www.texture;
  205. #else
  206. Texture2D texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
  207. #endif
  208. www = null;
  209. return texture;
  210. }
  211. public byte[] GetBytes()
  212. {
  213. redoCount = 0;
  214. #if UNITY_5
  215. byte[] bytes = www.bytes;
  216. #else
  217. byte[] bytes = www.downloadHandler.data;
  218. #endif
  219. www = null;
  220. return bytes;
  221. }
  222. }
  223. }