public void DownloadSave(string url, OnTextLoaded onTextLoaded, OnDownloadProgress onDownloadProgress) { //if (coWebReq != null) // StopCoroutine(coWebReq); //coWebReq = StartCoroutine(RequestText(url, 60f, onTextLoaded, onDownloadProgress)); }
public void LoadTextFile(LoadFileSettings settings, OnTextLoaded onResult, Encoding encoding = null) { if (encoding == null) { encoding = Encoding.Default; } StartCoroutine(_LoadTextFile(settings, onResult, encoding)); }
private IEnumerator _LoadTextFile(LoadFileSettings settings, OnTextLoaded onResult, Encoding encoding) { string result = ""; try { // Create a new StreamReader StreamReader theReader = new StreamReader(settings.fullPath, encoding); using (theReader) { result = theReader.ReadToEnd(); theReader.Close(); } }catch (Exception e) { Console.WriteLine("{0}\n", e.Message); } if (onResult != null) { onResult(result); } yield return(null); }
IEnumerator RequestText(string url, float timeOut, OnTextLoaded onTextLoaded, OnDownloadProgress onDownloadProgress) { UnityWebRequest webRequest = UnityWebRequest.Get(url); UnityWebRequestAsyncOperation handler = webRequest.SendWebRequest(); float timeIn = 0f; bool isAborted = false; while (!handler.isDone) { timeIn += Time.deltaTime; if (onDownloadProgress != null) { onDownloadProgress(handler.progress); } if (timeIn > timeOut) { //Security isAborted = true; webRequest.Abort(); break; } yield return(null); } if (webRequest.isNetworkError || webRequest.isHttpError || isAborted) { Debug.Log(webRequest.error); onTextLoaded(null); } else { string textRequested = webRequest.downloadHandler.text; onTextLoaded(textRequested); } yield break; }