123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using UnityEngine;
- using UnityEngine.Networking;
- namespace WorldComposer
- {
- public class WebRequest
- {
- const float DEFAULT_DELAY_REQUEST = 0.1f;
- const float PAUSE_DELAY_REQUEST = 1.1f;
- static List<WebRequest> requests = new List<WebRequest>();
- static float tStamp;
- static float delayRequest = DEFAULT_DELAY_REQUEST;
- #if UNITY_5
- public WWW www;
- #else
- public UnityWebRequest www;
- #endif
- string url;
- bool isTextureRequest;
- bool isAddedToList;
- int redoCount = 0;
- public static void ResetStampTime()
- {
- tStamp = -1000;
- }
- public static void PauseRequests()
- {
- delayRequest = PAUSE_DELAY_REQUEST;
- }
- static public void ProcessRequests()
- {
- if (requests.Count == 0) return;
- if (Time.realtimeSinceStartup - tStamp < delayRequest) return;
- tStamp = Time.realtimeSinceStartup;
- if (delayRequest > DEFAULT_DELAY_REQUEST + 0.01f)
- {
- Debug.Log("Resuming requests.");
- delayRequest = DEFAULT_DELAY_REQUEST;
- }
- try
- {
- SendWebRequest(requests[0]);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- requests[0].isAddedToList = false;
- requests.RemoveAt(0);
- }
- static public void SendWebRequest(WebRequest webRequest)
- {
- #if UNITY_5
- webRequest.www = new WWW(webRequest.url);
- #else
- #if UNITY_2017_1
- webRequest.www.Send();
- #else
- webRequest.www.SendWebRequest();
- #endif
- #endif
- }
- public void Request(string url, bool isTextureRequest)
- {
- url = url.ToString(CultureInfo.InvariantCulture);
- if (www != null)
- {
- if (isAddedToList)
- {
- requests.Remove(this);
- isAddedToList = false;
- }
- www.Dispose();
- www = null;
- }
- this.url = url;
- this.isTextureRequest = isTextureRequest;
- #if !UNITY_5
- if (isTextureRequest)
- {
- www = UnityWebRequestTexture.GetTexture(url);
- }
- else www = UnityWebRequest.Get(url);
- #endif
- if (Time.realtimeSinceStartup - tStamp < delayRequest)
- {
- isAddedToList = true;
- requests.Add(this);
- return;
- }
- tStamp = Time.realtimeSinceStartup;
- SendWebRequest(this);
- }
- public bool HasRequested
- {
- get
- {
- return www != null;
- }
- }
- public bool IsDone
- {
- get
- {
- if (www == null) return false;
- return www.isDone;
- }
- }
- public bool IsDoneIfErrorRedo
- {
- get
- {
- if (www != null && www.isDone)
- {
- #if UNITY_5
- if (!string.IsNullOrEmpty(www.error))
- #elif UNITY_2020_1_OR_NEWER
- if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.DataProcessingError || www.result == UnityWebRequest.Result.ProtocolError)
- #else
- if (www.isNetworkError || www.isHttpError)
- #endif
- {
- if (redoCount++ <= 3)
- {
- if (redoCount == 4)
- {
- Debug.LogError("Check if your Bing key is corrent, see the WorldComposer window how to solve it.");
- }
- #if UNITY_2020_1_OR_NEWER
- else Debug.LogError(www.error + " response code " + www.responseCode + " www.result " + www.result);
- #else
- else Debug.LogError(www.error + " response code " + www.responseCode + " isNetworkError " + www.isNetworkError + " isHttpError " + www.isHttpError);
- #endif
- RedoRequest();
- }
- return false;
- }
- return true;
- }
- return false;
- }
- }
- public bool IsError(out string text)
- {
- #if UNITY_5
- bool isError = !string.IsNullOrEmpty(www.error);
- #elif UNITY_2020_1_OR_NEWER
- bool isError = www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.DataProcessingError || www.result == UnityWebRequest.Result.ProtocolError;
- #else
- bool isError = www.isNetworkError || www.isHttpError;
- #endif
- if (isError)
- {
- text = www.error;
- www = null;
- }
- else text = string.Empty;
- return isError;
- }
- public void RedoRequest()
- {
- Request(url, isTextureRequest);
- }
- public void Abort()
- {
- if (www != null && !www.isDone)
- {
- #if UNITY_5
- www.Dispose();
- #else
- www.Abort();
- #endif
-
- www = null;
- }
- }
- public string GetText()
- {
- redoCount = 0;
- if (www == null)
- {
- Debug.LogError("www = null");
- return string.Empty;
- }
- #if UNITY_5
- string text = www.text;
- #else
- if (www.downloadHandler == null)
- {
- Debug.LogError("downloadHandeler = null");
- return string.Empty;
- }
- string text = www.downloadHandler.text;
- #endif
- www = null;
- return text;
- }
- public Texture2D GetTexture()
- {
- redoCount = 0;
- #if UNITY_5
- Texture2D texture = www.texture;
- #else
- Texture2D texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
- #endif
-
- www = null;
- return texture;
- }
- public byte[] GetBytes()
- {
- redoCount = 0;
- #if UNITY_5
- byte[] bytes = www.bytes;
- #else
- byte[] bytes = www.downloadHandler.data;
- #endif
- www = null;
- return bytes;
- }
- }
- }
|