Exemplo n.º 1
0
 public void DownloadSave(string url, OnTextLoaded onTextLoaded, OnDownloadProgress onDownloadProgress)
 {
     //if (coWebReq != null)
     //    StopCoroutine(coWebReq);
     //coWebReq =
     StartCoroutine(RequestText(url, 60f, onTextLoaded, onDownloadProgress));
 }
Exemplo n.º 2
0
 public void LoadTextFile(LoadFileSettings settings, OnTextLoaded onResult, Encoding encoding = null)
 {
     if (encoding == null)
     {
         encoding = Encoding.Default;
     }
     StartCoroutine(_LoadTextFile(settings, onResult, encoding));
 }
Exemplo n.º 3
0
    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);
    }
Exemplo n.º 4
0
    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;
    }