示例#1
0
//		public static IEnumerator start ( IEnumerator _routine, MonoBehaviour owner)
//		{
//			if (!Application.isPlaying) {
//				GORoutine coroutine = new GORoutine(_routine);
//				coroutine.start();
//				yield return coroutine;
//			} else {
//				yield return owner.StartCoroutine (_routine);
//			}
//		}


        public static GORoutine start(UnityWebRequest www, MonoBehaviour owner)
        {
            if (!Application.isPlaying)
            {
                GORoutine coroutine = new GORoutine(www);
                coroutine.start();
                return(coroutine);
            }
            else
            {
                owner.StartCoroutine(HandleUnityWebRequest(www));
                return(null);
            }
        }
示例#2
0
 public static GORoutine start(IEnumerator _routine, MonoBehaviour owner)
 {
     if (!Application.isPlaying)
     {
         GORoutine coroutine = new GORoutine(_routine);
         coroutine.start();
         return(coroutine);
     }
     else
     {
         owner.StartCoroutine(_routine);
         return(null);
     }
 }
        public static IEnumerator getRequest(MonoBehaviour host, string url, bool useCache, string filename, Action <byte[], string, string> response)
        {
            if (Application.isPlaying)               //Runtime build

            {
                if (useCache && FileHandler.Exist(filename))
                {
                    byte[] file = FileHandler.Load(filename);
                    response(file, null, null);
                }
                else
                {
                    var www = new WWW(url);
                    yield return(www);

                    if (string.IsNullOrEmpty(www.error) && www.bytes.Length > 0)
                    {
                        Debug.Log("[GOUrlRequest]  " + url);
                        if (useCache)
                        {
                            FileHandler.Save(filename, www.bytes);
                        }
                    }
                    else if (www.error != null && (www.error.Contains("429") || www.error.Contains("timed out")))
                    {
                        Debug.LogWarning("[GOUrlRequest] data reload " + www.error + " " + url);
                        yield return(new WaitForSeconds(1));

                        yield return(host.StartCoroutine(getRequest(host, url, useCache, filename, response)));

                        yield break;
                    }
                    else
                    {
                        Debug.LogWarning("[GOUrlRequest] Tile data missing " + www.error + " " + url);
                        response(null, null, www.error);
                        yield break;
                    }

//					byte[] image = www.bytes;
                    response(www.bytes, www.text, www.error);
                }
            }
            else               //Editor build

            {
                if (useCache && FileHandler.Exist(filename))
                {
                    byte[] file = FileHandler.Load(filename);
                    response(file, null, null);
                }
                else
                {
                        #if UNITY_EDITOR
                    var www = new WWW(url);

                    ContinuationManager.Add(() => www.isDone, () => {
                        if (string.IsNullOrEmpty(www.error) && www.bytes.Length > 0)
                        {
                            Debug.Log("[GOUrlRequest]  " + url);
                            if (useCache)
                            {
                                FileHandler.Save(filename, www.bytes);
                            }
                            response(www.bytes, www.text, null);
                        }
                        else if (www.error != null && (www.error.Contains("429") || www.error.Contains("timed out")))
                        {
                            Debug.LogWarning("[GOUrlRequest] data reload " + www.error);
                            System.Threading.Thread.Sleep(1000);
                            GORoutine.start(getRequest(host, url, useCache, filename, response), host);
                        }
                        else
                        {
                            Debug.LogWarning("[GOUrlRequest] data missing " + www.error + " url: " + url);
                            response(null, null, www.error);
                        }
                    });
                        #endif
                    yield break;
                }
            }

            yield return(null);
        }
        public static IEnumerator jsonRequest(MonoBehaviour host, string url, bool useCache, string filename, Action <Dictionary <string, object>, string> response)
        {
            ParseJob job = new ParseJob();

            if (Application.isPlaying)               //Runtime build

            {
                if (useCache && FileHandler.Exist(filename))
                {
                    job.InData = FileHandler.LoadText(filename);
                    job.Start();
                    yield return(host.StartCoroutine(job.WaitFor()));

                    response((Dictionary <string, object>)job.OutData, null);
                }
                else
                {
                    var www = new WWW(url);
                    yield return(www);

                    if (string.IsNullOrEmpty(www.error) && www.bytes.Length > 0)
                    {
                        Debug.Log("[GOUrlRequest]  " + url);
                        if (useCache)
                        {
                            FileHandler.Save(filename, www.bytes);
                        }
                    }
                    else if (www.error != null && (www.error.Contains("429") || www.error.Contains("timed out")))
                    {
                        Debug.LogWarning("[GOUrlRequest] data reload " + www.error);
                        yield return(new WaitForSeconds(1));

                        yield return(host.StartCoroutine(jsonRequest(host, url, useCache, filename, response)));

                        yield break;
                    }
                    else
                    {
                        Debug.LogWarning("[GOUrlRequest] data missing " + www.error + " " + url);
                        response(null, www.error);
                        yield break;
                    }


                    job.InData = www.text;                     //FileHandler.LoadText (filename);
                    job.Start();
                    yield return(host.StartCoroutine(job.WaitFor()));

                    response((Dictionary <string, object>)job.OutData, null);
                }
            }
            else               //Editor build

            {
                if (useCache && FileHandler.Exist(filename))
                {
                    response((Dictionary <string, object>)Json.Deserialize(FileHandler.LoadText(filename)), null);
                }
                else
                {
                        #if UNITY_EDITOR
                    var www = new WWW(url);

                    ContinuationManager.Add(() => www.isDone, () => {
                        if (String.IsNullOrEmpty(www.error) && www.bytes.Length > 0)
                        {
                            Debug.Log("[GOUrlRequest]  " + url);
                            if (useCache)
                            {
                                FileHandler.Save(filename, www.bytes);
                            }
                            response((Dictionary <string, object>)Json.Deserialize(
                                         FileHandler.LoadText(filename)), null);
                        }
                        else if (!String.IsNullOrEmpty(www.error) && (www.error.Contains("429") || www.error.Contains("timed out")))
                        {
                            Debug.LogWarning("[GOUrlRequest] data reload " + www.error);
                            System.Threading.Thread.Sleep(1000);
                            GORoutine.start(jsonRequest(host, url, useCache, filename, response), host);
                        }
                        else
                        {
                            Debug.LogWarning("[GOUrlRequest] Tile data missing " + www.error);
                            response(null, www.error);
                        }
                    });
                        #endif
                    yield break;
                }
            }
            yield return(null);
        }