コード例 #1
0
        public IEnumerator ParseJson(string data)
        {
            job        = new ParseJob();
            job.InData = data;
            job.Start();

            yield return(StartCoroutine(job.WaitFor()));
        }
コード例 #2
0
        public IEnumerator LoadPlaces(string url)            //Request the API
        {
            Debug.Log("GO PLACES URL: " + url);

            var www = new WWW(url);

            yield return(www);

            ParseJob job = new ParseJob();

            job.InData = www.text;
            job.Start();

            yield return(StartCoroutine(job.WaitFor()));

            IDictionary response = (IDictionary)job.OutData;

            IList results = (IList)response ["results"];

//			foreach (Transform child in transform) {
//				GameObject.Destroy (child.gameObject);
//			}


            foreach (IDictionary result in results)               //This example only takes GPS location and the id of the object. There's lot more, take a look at the places API documentation

            {
                IDictionary location = (IDictionary)((IDictionary)result ["geometry"])["location"];
                double      lat      = (double)location ["lat"];
                double      lng      = (double)location ["lng"];

                //			GameObject go = GameObject.Instantiate (prefab);
                //			go.name = (string)result["place_id"];
                //			goMap.dropPin (lat, lng, go);

                Coordinates coordinates = new Coordinates(lat, lng, 0);
                GameObject  go          = GameObject.Instantiate(prefab);
                Vector3     pos         = coordinates.convertCoordinateToVector(0);

                                #if GOLINK
                pos.y = GoTerrain.GOTerrain.RaycastAltitudeForVector(pos);
                                #endif

                go.transform.localPosition = pos;

                go.transform.parent = transform;
                go.name             = (string)result["place_id"];
            }
        }
コード例 #3
0
        public IEnumerator LoadPlaces(string url)            //Request the API
        {
            Debug.Log("GO4Square URL: " + url);

            var www = new WWW(url);

            yield return(www);

            ParseJob job = new ParseJob();

            job.InData = www.text;
            job.Start();

            yield return(StartCoroutine(job.WaitFor()));

            IDictionary response = (IDictionary)((IDictionary)job.OutData)["response"];
            IList       results  = (IList)response ["venues"];

            foreach (Transform child in transform)
            {
                GameObject.Destroy(child.gameObject);
            }

            foreach (IDictionary result in results)              //This example only takes GPS location and the name of the object. There's lot more, take a look at the Foursquare API documentation

            {
                IDictionary location = ((IDictionary)result ["location"]);
                double      lat      = (double)location ["lat"];
                double      lng      = (double)location ["lng"];


                Coordinates coordinates = new Coordinates(lat, lng, 0);
                GameObject  go          = GameObject.Instantiate(prefab);
                Vector3     pos         = coordinates.convertCoordinateToVector(0);

                if (goMap.useElevation)
                {
                    pos = GOMap.AltitudeToPoint(pos);
                }

                go.transform.localPosition = pos;

                go.transform.parent = transform;
                go.name             = (string)result["name"];
            }
        }
コード例 #4
0
        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);
        }