public static void AddMap() { GameObject locationManager = CreateGameObjectInScene("Location Manager"); CenterOnScreen(locationManager, 0); GameObject newMap = CreateGameObjectInScene("Map"); CenterOnScreen(newMap, 0); GOMap map = newMap.AddComponent <GOMap>(); map.locationManager = locationManager.AddComponent <LocationManager>(); Layer buildings = new Layer(); buildings.name = "Buildings"; buildings.json = "buildings"; buildings.isPolygon = true; buildings.defaultHeight = 2; Layer roads = new Layer(); roads.name = "Roads"; roads.json = "roads"; roads.isPolygon = false; roads.defaultWidth = 2; map.layers = new Layer[] { buildings, roads }; Camera.main.transform.position = new Vector3(0, 600, 100); Camera.main.transform.eulerAngles = new Vector3(70, 180, 0); }
public void DestroyCurrentMap() { GOMap map = GetComponent <GOMap> (); if (map == null) { Debug.LogError("[GOMap Editor] GOMap script not found"); return; } while (map.transform.childCount > 0) { foreach (Transform child in map.transform) { GameObject.DestroyImmediate(child.gameObject); } } GOEnvironment env = GameObject.FindObjectOfType <GOEnvironment>(); if (env == null) { return; } while (env.transform.childCount > 0) { foreach (Transform child in env.transform) { GameObject.DestroyImmediate(child.gameObject); } } }
public void TestUnityWebRequestInEditor() { GOMap map = GetComponent <GOMap> (); if (map == null) { Debug.LogError("[GOMap Editor] GOMap script not found! GOMapEditor and GOMap have to be attached to the same GameObject"); return; } map.TestEditorUnityWebRequest(); }
public void LoadInsideEditor() { GOMap map = GetComponent <GOMap> (); if (map == null) { Debug.LogError("[GOMap Editor] GOMap script not found"); return; } map.BuildInsideEditor(); }
public static void AddMap() { GameObject locationManager = CreateGameObjectInScene("Location Manager"); CenterOnScreen(locationManager, 0); GameObject newMap = CreateGameObjectInScene("Map"); CenterOnScreen(newMap, 0); GOMap map = newMap.AddComponent <GOMap> (); map.locationManager = locationManager.AddComponent <LocationManager>(); GOLayer buildings = new GOLayer(); buildings.layerType = GOLayer.GOLayerType.Buildings; buildings.isPolygon = true; buildings.defaultRendering = new GORenderingOptions(); buildings.defaultRendering.polygonHeight = 2; buildings.defaultRendering.material = CreateMaterialWithColor(Color.white); GOLayer water = new GOLayer(); buildings.layerType = GOLayer.GOLayerType.Water; water.isPolygon = true; water.defaultRendering = new GORenderingOptions(); water.defaultRendering.polygonHeight = 0; water.defaultRendering.material = CreateMaterialWithColor(Color.blue); GOLayer landuse = new GOLayer(); buildings.layerType = GOLayer.GOLayerType.Landuse; landuse.isPolygon = true; landuse.defaultRendering = new GORenderingOptions(); landuse.defaultRendering.polygonHeight = 0; landuse.defaultRendering.material = CreateMaterialWithColor(Color.green); GOLayer roads = new GOLayer(); buildings.layerType = GOLayer.GOLayerType.Landuse; roads.isPolygon = false; roads.defaultRendering = new GORenderingOptions(); roads.defaultRendering.lineWidth = 2; roads.defaultRendering.material = CreateMaterialWithColor(Color.gray); map.layers = new GOLayer[] { buildings, water, landuse, roads }; Camera.main.transform.position = new Vector3(0, 600, 100); Camera.main.transform.eulerAngles = new Vector3(70, 180, 0); }
public IEnumerator SpawnPrefabsInMesh(GOFeature feature, GameObject parent, GOEnvironmentKind kind) { if (feature.preloadedMeshData == null) { yield break; } float area = Area(feature.convertedGeometry); float rate = kind.density / 1000.0f; int k = Mathf.FloorToInt(area * rate / 100); //Debug.Log (area + " " + rate + " " + k); for (int i = 0; i < k; i++) { try { // int spawn = UnityEngine.Random.Range (0, rate); // if (spawn != 0) // continue; int n = UnityEngine.Random.Range(0, kind.prefabs.Length); Vector3 pos = randomPointInShape(feature.convertedGeometry); if (GOMap.IsPointAboveWater(pos)) { continue; } pos.y += kind.prefabs [n].transform.position.y; var rotation = kind.prefabs [n].transform.eulerAngles; var randomRotation = new Vector3(0, UnityEngine.Random.Range(0, 360), 0); GameObject obj = (GameObject)GameObject.Instantiate(kind.prefabs[n], pos, Quaternion.Euler(rotation + randomRotation)); obj.transform.parent = parent.transform; } catch { } if (Application.isPlaying) { yield return(null); } } yield return(null); }
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"]; } }
//Draws a line given a list of latitudes/longitudes public GameObject dropLine(List <Coordinates> polyline, float width, float height, Material material, GOUVMappingStyle uvMappingStyle) { List <Vector3> converted = new List <Vector3> (); foreach (Coordinates coordinates in polyline) { Vector3 v = coordinates.convertCoordinateToVector(); if (useElevation) { v = GOMap.AltitudeToPoint(v); } converted.Add(v); } return(dropLine(converted, width, height, material, uvMappingStyle)); }
public IEnumerator SpawnPrefabsIn3DMesh(GOFeature feature, GameObject parent, GOEnvironmentKind kind) { if (feature.preloadedMeshData == null) { yield break; } int rate = 100 / kind.density; foreach (Vector3 vertex in feature.preloadedMeshData.vertices) { try { int spawn = UnityEngine.Random.Range(0, rate); if (spawn != 0) { continue; } int n = UnityEngine.Random.Range(0, kind.prefabs.Length); Vector3 pos = vertex; if (GOMap.IsPointAboveWater(pos)) { continue; } pos.y += kind.prefabs [n].transform.position.y; var rotation = kind.prefabs [n].transform.eulerAngles; var randomRotation = new Vector3(0, UnityEngine.Random.Range(0, 360), 0); GameObject obj = (GameObject)GameObject.Instantiate(kind.prefabs[n], pos, Quaternion.Euler(rotation + randomRotation)); obj.transform.parent = parent.transform; } catch { } if (Application.isPlaying) { yield return(null); } } yield return(null); }
public IEnumerator LoadTileData(GOMap m, Coordinates tilecenter, int zoom, GOLayer[] layers, bool delayedLoad) { map = m; tileCoordinates = tileCenter.tileCoordinates(map.zoomLevel); if (Application.isPlaying) { yield return(StartCoroutine(DownloadData(m, tileCenter, zoom, layers, delayedLoad))); List <string> layerNames = map.layerNames(); yield return(StartCoroutine(ParseTileData(map, tileCenter, zoom, layers, delayedLoad, layerNames))); } else { GORoutine.start(DownloadData(m, tileCenter, zoom, layers, delayedLoad), this); } }
//In the update we detect taps of mouse or touch to trigger a raycast on the ground void Update() { bool drag = false; if (Application.isMobilePlatform) { drag = Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began; } else { drag = Input.GetMouseButton(0); } if (drag) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, Mathf.Infinity, GOMap.GetActiveMasks())) { //From the raycast data it's easy to get the vector3 of the hit point Vector3 worldVector = hit.point; //And it's just as easy to get the gps coordinate of the hit point. Coordinates gpsCoordinates = Coordinates.convertVectorToCoordinates(hit.point); if (debugLog) { //There's a little debug string Debug.Log(string.Format("[GOMap] Tap world vector: {0}, GPS Coordinates: {1}", worldVector, gpsCoordinates.toLatLongString())); } if (currentProjector != null && atomic) { GameObject.Destroy(currentProjector); } //Add a simple projector to the tapped point currentProjector = GameObject.Instantiate(projectorPrefab); worldVector.y += 5.5f; currentProjector.transform.position = worldVector; } } }
//Draws a polygon given a list of latitudes/longitudes that is a closed shape public GameObject dropPolygon(List <Coordinates> shape, float height, Material material, GOUVMappingStyle uvMappingStyle) { List <Vector3> converted = new List <Vector3> (); foreach (Coordinates coordinates in shape) { Vector3 v = coordinates.convertCoordinateToVector(); if (useElevation) { v = GOMap.AltitudeToPoint(v); } converted.Add(v); } if (!GOFeature.IsClockwise(converted)) { converted.Reverse(); } return(dropPolygon(converted, height, material, uvMappingStyle)); }
public IEnumerator SpawnPrefabsInTile(GOTile tile, GameObject parent, GOEnvironmentKind kind) { if (tile == null) { yield break; } float rate = Mathf.FloorToInt(tile.goTile.diagonalLenght * (kind.density / 100)); for (int i = 0; i <= rate; i++) { float randomX = UnityEngine.Random.Range(tile.goTile.getXRange().x, tile.goTile.getXRange().y); float randomZ = UnityEngine.Random.Range(tile.goTile.getZRange().x, tile.goTile.getZRange().y); float randomY = UnityEngine.Random.Range(200, 320); int n = UnityEngine.Random.Range(0, kind.prefabs.Length); Vector3 pos = new Vector3(randomX, randomY, randomZ); pos.y += kind.prefabs [n].transform.position.y; pos.y += GOMap.AltitudeForPoint(pos); var rotation = kind.prefabs [n].transform.eulerAngles; var randomRotation = new Vector3(0, UnityEngine.Random.Range(0, 360), 0); GameObject obj = (GameObject)GameObject.Instantiate(kind.prefabs[n], pos, Quaternion.Euler(rotation + randomRotation)); obj.transform.parent = parent.transform; obj.transform.position = pos; if (Application.isPlaying) { yield return(null); } } yield return(null); }
IEnumerator NearbySearch(GOTile tile) { //Center of the map tile Coordinates tileCenter = tile.goTile.tileCenter; //radius of the request, equals the tile diagonal /2 float radius = tile.goTile.diagonalLenght / 2; //The complete nearby search url, api key is added at the end string url = nearbySearchUrl + "location=" + tile.goTile.tileCenter.latitude + "," + tile.goTile.tileCenter.longitude + "&radius=" + radius + "&type=" + type + "&key=" + googleAPIkey; //Perform the request var www = UnityWebRequest.Get(url); yield return(www.SendWebRequest()); //Check for errors if (string.IsNullOrEmpty(www.error)) { string response = www.downloadHandler.text; //Deserialize the json response IDictionary deserializedResponse = (IDictionary)Json.Deserialize(response); Debug.Log(string.Format("[GO Places] Tile center: {0} - Request Url {1} - response {2}", tileCenter.toLatLongString(), url, response)); //That's our list of Places IList results = (IList)deserializedResponse ["results"]; //Create a container for the places and set it as a tile child. In this way when the tile is destroyed it will take also the places with it. GameObject placesContainer = new GameObject("Places"); placesContainer.transform.SetParent(tile.transform); foreach (IDictionary result in results) { string placeID = (string)result["place_id"]; string name = (string)result["name"]; IDictionary location = (IDictionary)((IDictionary)result ["geometry"])["location"]; double lat = (double)location ["lat"]; double lng = (double)location ["lng"]; //Create a new coordinate object, with the desired lat lon Coordinates coordinates = new Coordinates(lat, lng, 0); if (!TileFilter(tile, coordinates)) { continue; } //Instantiate your game object GameObject place = GameObject.Instantiate(prefab); place.SetActive(true); //Convert coordinates to position Vector3 position = coordinates.convertCoordinateToVector(place.transform.position.y); if (goMap.useElevation) { position = GOMap.AltitudeToPoint(position); } //Set the position to object place.transform.localPosition = position; //the parent place.transform.SetParent(placesContainer.transform); //and the name place.name = (name != null && name.Length > 0)? name:placeID; if (addGOPlaceComponent) { GOPlacesPrefab component = place.AddComponent <GOPlacesPrefab> (); component.placeInfo = result; component.goPlaces = this; } } } }
public GameObject BuildLine(GOLayer layer, GORenderingOptions renderingOptions, GOMap map, GameObject parent) { if (feature.convertedGeometry.Count == 2 && feature.convertedGeometry[0].Equals(feature.convertedGeometry[1])) { return(null); } GameObject line = GameObject.Instantiate(feature.goTile.featurePrototype, parent.transform); if (renderingOptions.tag.Length > 0) { line.tag = renderingOptions.tag; } if (renderingOptions.material) { renderingOptions.material.renderQueue = -(int)feature.sort; } if (renderingOptions.outlineMaterial) { renderingOptions.outlineMaterial.renderQueue = -(int)feature.sort; } GOLineMesh lineMesh = new GOLineMesh(feature.convertedGeometry); lineMesh.width = renderingOptions.lineWidth; lineMesh.load(line); mesh = lineMesh.mesh; line.GetComponent <Renderer>().material = renderingOptions.material; Vector3 position = line.transform.position; position.y = feature.y; line.transform.position = position; if (renderingOptions.outlineMaterial != null) { GameObject outline = CreateRoadOutline(line, renderingOptions.outlineMaterial, renderingOptions.lineWidth + layer.defaultRendering.outlineWidth); if (layer.useColliders) { MeshCollider mc = outline.GetComponent <MeshCollider> (); mc.enabled = true; mc.sharedMesh = outline.GetComponent <MeshFilter> ().sharedMesh; } outline.layer = line.layer; outline.tag = line.tag; } else if (layer.useColliders) { line.GetComponent <MeshCollider> ().enabled = true; } return(line); }
public void BuildLineFromPreloaded(GameObject line, GOFeature feature, GOMap map) { if (feature.preloadedMeshData == null) { return; } GORenderingOptions renderingOptions = feature.renderingOptions; if (renderingOptions.tag.Length > 0) { line.tag = renderingOptions.tag; } if (renderingOptions.material) { renderingOptions.material.renderQueue = -(int)feature.sort; } if (renderingOptions.outlineMaterial) { renderingOptions.outlineMaterial.renderQueue = -(int)feature.sort; } MeshFilter filter = line.GetComponent <MeshFilter> (); if (filter == null) { filter = (MeshFilter)line.AddComponent(typeof(MeshFilter)); } MeshRenderer renderer = line.GetComponent <MeshRenderer> (); if (renderer == null) { renderer = (MeshRenderer)line.AddComponent(typeof(MeshRenderer)); } filter.sharedMesh = feature.preloadedMeshData.ToMesh(); line.GetComponent <Renderer>().material = renderingOptions.material; Vector3 position = line.transform.position; position.y = feature.y; position.y += Noise(); if (GOMap.GOLink && renderingOptions.polygonHeight > 0) { position.y -= GOFeature.BuildingElevationOffset; } line.transform.position = position; if (renderingOptions.outlineMaterial != null && feature.preloadedMeshData != null && feature.preloadedMeshData.secondaryMesh != null) { GameObject outline = RoadOutlineFromPreloaded(line, feature, renderingOptions.outlineMaterial); if (feature.layer.useColliders) { outline.AddComponent <MeshCollider> ().sharedMesh = outline.GetComponent <MeshFilter> ().sharedMesh; } outline.layer = line.layer; outline.tag = line.tag; } else if (feature.layer.useColliders) { line.AddComponent <MeshCollider> (); } }
public IEnumerator LoadTileData(object m, Coordinates tilecenter, int zoom, Layer[] layers, bool delayedLoad) { #if !UNITY_WEBPLAYER map = (GOMap)m; Vector2 realPos = tileCenter.tileCoordinates(zoom); var tileurl = realPos.x + "/" + realPos.y; var baseUrl = "https://tile.mapzen.com/mapzen/vector/v1/"; // var baseUrl = "https://vector.mapzen.com/osm/"; oldapi List <string> layerNames = new List <string>(); for (int i = 0; i < layers.ToList().Count; i++) { if (layers[i].disabled == false) { layerNames.Add(layers[i].json); } } layerNames.RemoveAll(str => String.IsNullOrEmpty(str)); // var url = baseUrl + string.Join(",",layerNames.ToArray())+"/"+zoom+"/"; var url = baseUrl + "all/" + zoom + "/"; var completeurl = url + tileurl + ".json"; if (map.mapzen_api_key != null && map.mapzen_api_key != "") { completeurl = completeurl + "?api_key=" + map.mapzen_api_key; } if (Application.isPlaying) //Runtime build { if (map.useCache && FileHandler.Exist(gameObject.name)) { yield return(StartCoroutine(ParseJson(FileHandler.LoadText(gameObject.name)))); } else { Debug.Log(completeurl); var www = new WWW(completeurl); yield return(www); if (www.error == null && www.text.Length > 0) { FileHandler.SaveText(gameObject.name, www.text); } else if (www.error != null && (www.error.Contains("429") || www.error.Contains("timed out"))) { Debug.LogWarning("Tile data reload " + www.error); yield return(new WaitForSeconds(1)); yield return(StartCoroutine(LoadTileData(map, tilecenter, zoom, layers, delayedLoad))); yield break; } else { Debug.LogWarning("Tile data missing " + www.error); ((GOMap)m).tiles.Remove(this); GameObject.Destroy(this.gameObject); yield break; } yield return(StartCoroutine(ParseJson(www.text))); } mapData = job.OutData; yield return(StartCoroutine(ParseTileData(map, tileCenter, zoom, layers, delayedLoad, layerNames))); } else //Editor build { if (map.useCache && FileHandler.Exist(gameObject.name)) { mapData = Json.Deserialize(FileHandler.LoadText(gameObject.name)); GORoutine.start(ParseTileData(map, tileCenter, zoom, layers, delayedLoad, layerNames), this); } else { #if UNITY_EDITOR WWW www = new WWW(completeurl); ContinuationManager.Add(() => www.isDone, () => { if (!string.IsNullOrEmpty(www.error)) { Debug.LogWarning("Tile data missing " + www.error); System.Threading.Thread.Sleep(1000); GORoutine.start(LoadTileData(map, tilecenter, zoom, layers, delayedLoad), this); } else if (this != null) { FileHandler.SaveText(gameObject.name, www.text); mapData = Json.Deserialize(FileHandler.LoadText(gameObject.name)); GORoutine.start(ParseTileData(map, tileCenter, zoom, layers, delayedLoad, layerNames), this); } }); #endif yield break; } } #else yield return(null); #endif }
public GameObject BuildLineFromPreloaded(GOFeature feature, GOMap map, GameObject parent) { if (feature.preloadedMeshData == null) { return(null); } GameObject line = GameObject.Instantiate(feature.goTile.featurePrototype, parent.transform); GORenderingOptions renderingOptions = feature.renderingOptions; if (renderingOptions.tag.Length > 0) { line.tag = renderingOptions.tag; } if (renderingOptions.material) { renderingOptions.material.renderQueue = -(int)feature.sort; } if (renderingOptions.outlineMaterial) { renderingOptions.outlineMaterial.renderQueue = -(int)feature.sort; } MeshFilter filter = line.GetComponent <MeshFilter> (); MeshRenderer renderer = line.GetComponent <MeshRenderer> (); renderer.shadowCastingMode = feature.layer.castShadows; filter.sharedMesh = feature.preloadedMeshData.ToMesh(); renderer.material = renderingOptions.material; // filter.sharedMesh = feature.preloadedMeshData.ToRoadMesh(); // renderer.materials = new Material[] {renderingOptions.material,renderingOptions.outlineMaterial}; Vector3 position = line.transform.position; position.y = feature.y; position.y += Noise(); position.y *= feature.goTile.worldScale; if (feature.goTile.useElevation) { position.y -= GOFeature.BuildingElevationOffset; } line.transform.position = position; if (renderingOptions.outlineMaterial != null && feature.preloadedMeshData != null && feature.preloadedMeshData.secondaryMesh != null) { GameObject outline = RoadOutlineFromPreloaded(line, feature, renderingOptions.outlineMaterial); if (feature.layer.useColliders) { MeshCollider mc = outline.GetComponent <MeshCollider> (); mc.enabled = true; mc.sharedMesh = outline.GetComponent <MeshFilter> ().sharedMesh; } outline.layer = line.layer; outline.tag = line.tag; } else if (feature.layer.useColliders) { MeshCollider collider = line.GetComponent <MeshCollider> (); collider.enabled = true; collider.sharedMesh = filter.sharedMesh; } return(line); }
public void BuildMapPortionInsideEditor() { GOMap map = GetComponent <GOMap>(); map.BuildMapPortionInsideEditor(location, null); }
public void BuildLine(GameObject line, GOLayer layer, GORenderingOptions renderingOptions, GOMap map) { if (feature.convertedGeometry.Count == 2 && feature.convertedGeometry[0].Equals(feature.convertedGeometry[1])) { return; } #if GOLINK feature.convertedGeometry = GOFeatureMeshBuilder.BreakLine(feature.convertedGeometry, map.goTerrain); #endif if (renderingOptions.tag.Length > 0) { line.tag = renderingOptions.tag; } if (renderingOptions.material) { renderingOptions.material.renderQueue = -(int)feature.sort; } if (renderingOptions.outlineMaterial) { renderingOptions.outlineMaterial.renderQueue = -(int)feature.sort; } GOLineMesh lineMesh = new GOLineMesh(feature.convertedGeometry); lineMesh.width = renderingOptions.lineWidth; lineMesh.load(line); mesh = lineMesh.mesh; line.GetComponent <Renderer>().material = renderingOptions.material; Vector3 position = line.transform.position; position.y = feature.y; #if GOLINK if (renderingOptions.polygonHeight > 0) { int offset = GOFeature.BuildingElevationOffset; line.GetComponent <MeshFilter> ().sharedMesh = SimpleExtruder.Extrude(line.GetComponent <MeshFilter> ().sharedMesh, line, renderingOptions.polygonHeight + offset); position.y -= offset; } #else #endif line.transform.position = position; if (renderingOptions.outlineMaterial != null) { GameObject outline = CreateRoadOutline(line, renderingOptions.outlineMaterial, renderingOptions.lineWidth + layer.defaultRendering.outlineWidth); if (layer.useColliders) { outline.AddComponent <MeshCollider> ().sharedMesh = outline.GetComponent <MeshFilter> ().sharedMesh; } outline.layer = line.layer; outline.tag = line.tag; } else if (layer.useColliders) { // Mesh m = gameObject.GetComponent<MeshFilter> ().sharedMesh; line.AddComponent <MeshCollider> (); } }