private void OnEnable() { #if UNITY_EDITOR EditorApplication.update += CheckScriptCompiling; #endif OnlineMapsUtils.persistentDataPath = Application.persistentDataPath; isPlaying = true; _instance = this; tooltipDrawer = new OnlineMapsGUITooltipDrawer(this); activeType = OnlineMapsProvider.FindMapType(mapType); _mapType = mapType = activeType.fullID; if (tileManager == null) { tileManager = new OnlineMapsTileManager(this); } trafficProvider = OnlineMapsTrafficProvider.GetByID(trafficProviderID); if (language == "") { language = activeType.provider.twoLetterLanguage ? "en" : "eng"; } _language = language; _labels = labels; _traffic = traffic; _trafficProviderID = trafficProviderID; izoom = (int)_zoom; UpdateCorners(); }
private void OnDisable() { OnlineMapsThreadManager.Dispose(); if (_buffer != null) { _buffer.Dispose(); _buffer = null; } #if NETFX_CORE if (renderThread != null) { renderThread.Dispose(); } #endif #if !UNITY_WEBGL renderThread = null; #endif if (tileManager != null) { tileManager.Dispose(); tileManager = null; } _control = null; if (_instance == this) { _instance = null; } }
private void OnStartDownloadTileM(OnlineMapsTile tile) { if (TryLoadFromCache(tile)) { if (OnLoadedFromCache != null) { OnLoadedFromCache(tile); } } else { #pragma warning disable 618 if (OnStartDownloadTile != null) { OnStartDownloadTile(tile); } #pragma warning restore 618 else if (OnlineMapsTileManager.OnStartDownloadTile != null) { OnlineMapsTileManager.OnStartDownloadTile(tile); } else { OnlineMapsTileManager.StartDownloadTile(tile); } } }
/// <summary> /// This method is called when loading the tile. /// </summary> /// <param name="tile">Reference to tile</param> private void OnStartDownloadTile(OnlineMapsTile tile) { // Get local path. string path = GetTilePath(tile); // If the tile is cached. if (File.Exists(path)) { // Load tile texture from cache. Texture2D tileTexture = new Texture2D(256, 256, TextureFormat.RGB24, false); tileTexture.LoadImage(File.ReadAllBytes(path)); tileTexture.wrapMode = TextureWrapMode.Clamp; // Send texture to map. if (OnlineMapsControlBase.instance.resultIsTexture) { (tile as OnlineMapsRasterTile).ApplyTexture(tileTexture); OnlineMaps.instance.buffer.ApplyTile(tile); OnlineMapsUtils.Destroy(tileTexture); } else { tile.texture = tileTexture; tile.status = OnlineMapsTileStatus.loaded; } // Redraw map. OnlineMaps.instance.Redraw(); } else { // If the tile is not cached, download tile with a standard loader. OnlineMapsTileManager.StartDownloadTile(tile); } }
private void OnStartDownloadTile(OnlineMapsTile tile) { // Load overlay for tile. LoadTileOverlay(tile); // Load the tile using a standard loader. OnlineMapsTileManager.StartDownloadTile(tile); }
public void Awake() { _instance = this; tileManager = new OnlineMapsTileManager(this); if (control == null) { Debug.LogError("Can not find a Control."); return; } if (control.resultIsTexture) { if (texture != null) { width = texture.width; height = texture.height; } } else { texture = null; } izoom = (int)floatZoom; UpdateCorners(); control.OnAwakeBefore(); if (control.resultIsTexture) { if (texture != null) { defaultColors = texture.GetPixels(); } if (defaultTileTexture == null) { OnlineMapsRasterTile.defaultColors = new Color32[OnlineMapsUtils.sqrTileSize]; for (int i = 0; i < OnlineMapsUtils.sqrTileSize; i++) { OnlineMapsRasterTile.defaultColors[i] = emptyColor; } } else { OnlineMapsRasterTile.defaultColors = defaultTileTexture.GetPixels32(); } } SetPosition(longitude, latitude); }
private void CheckBufferComplete() { if (buffer.status != OnlineMapsBufferStatus.complete) { return; } if (buffer.needUnloadTiles) { buffer.UnloadOldTiles(); } OnlineMapsTile.UnloadUnusedTiles(); if (allowRedraw) { if (control.resultIsTexture) { if (texture != null) { texture.SetPixels32(buffer.frontBuffer); texture.Apply(false); if (control.activeTexture != texture) { control.SetTexture(texture); } } } if (OnPreloadTiles != null) { OnPreloadTiles(this); } if (OnlineMapsTileManager.OnPreloadTiles != null) { OnlineMapsTileManager.OnPreloadTiles(); } if (control is OnlineMapsControlBase3D) { (control as OnlineMapsControlBase3D).UpdateControl(); } if (OnMapUpdated != null) { OnMapUpdated(); } } buffer.status = OnlineMapsBufferStatus.wait; }
private void Update() { if (OnUpdateBefore != null) { OnUpdateBefore(); } CheckBaseProps(); OnlineMapsTileManager.StartDownloading(); if (OnUpdateLate != null) { OnUpdateLate(); } }
private void OnDestroy() { OnlineMapsThreadManager.Dispose(); if (_buffer != null) { _buffer.Dispose(); _buffer = null; } #if NETFX_CORE if (renderThread != null) { renderThread.Dispose(); } #endif #if !UNITY_WEBGL renderThread = null; #endif if (tileManager != null) { tileManager.Dispose(); tileManager = null; } _control = null; if (defaultColors != null && texture != null) { if (texture.width * texture.height == defaultColors.Length) { texture.SetPixels(defaultColors); texture.Apply(); } } OnChangePosition = null; OnChangeZoom = null; OnMapUpdated = null; OnUpdateBefore = null; OnUpdateLate = null; }
private void OnStartDownloadTileM(OnlineMapsTile tile) { if (TryLoadFromCache(tile)) { if (OnLoadedFromCache != null) { OnLoadedFromCache(tile); } } else { if (OnStartDownloadTile != null) { OnStartDownloadTile(tile); } else { OnlineMapsTileManager.StartDownloadTile(tile); } } }
/// <summary> /// Marks the tile loaded. /// </summary> public void MarkLoaded() { if (OnlineMapsTileManager.OnTileLoaded != null) { OnlineMapsTileManager.OnTileLoaded(this); } if (OnAllTilesLoaded == null) { return; } foreach (OnlineMapsTile tile in tiles) { if (tile.status != OnlineMapsTileStatus.loaded && tile.status != OnlineMapsTileStatus.error) { return; } } OnAllTilesLoaded(); }
/// <summary> /// Constructor /// </summary> /// <param name="x">Tile X</param> /// <param name="y">Tile Y</param> /// <param name="zoom">Tile zoom</param> /// <param name="map">Reference to the map</param> /// <param name="isMapTile">Should this tile be displayed on the map?</param> public OnlineMapsTile(int x, int y, int zoom, OnlineMaps map, bool isMapTile = true) { remainDownloadsAttempts = countDownloadsAttempts; int maxX = 1 << zoom; if (x < 0) { x += maxX; } else if (x >= maxX) { x -= maxX; } this.x = x; this.y = y; this.zoom = zoom; _map = map; this.isMapTile = isMapTile; double tlx, tly, brx, bry; map.projection.TileToCoordinates(x, y, zoom, out tlx, out tly); map.projection.TileToCoordinates(x + 1, y + 1, zoom, out brx, out bry); topLeft = new OnlineMapsVector2d(tlx, tly); bottomRight = new OnlineMapsVector2d(brx, bry); globalPosition = Vector2.Lerp(topLeft, bottomRight, 0.5f); key = OnlineMapsTileManager.GetTileKey(zoom, x, y); if (isMapTile) { map.tileManager.Add(this); } }
private void OnStartDownloadTile(OnlineMapsTile tile) { OnlineMapsTileManager.StartDownloadTile(tile); StartDownloadOverlay(tile); }
protected override float GetUnscaledElevationValue(double x, double z, double tlx, double tly, double brx, double bry) { if (tiles == null) { tiles = new Dictionary <ulong, Tile>(); return(0); } x = x / -sizeInScene.x; z = z / sizeInScene.y; double ttlx, ttly, tbrx, tbry; map.projection.CoordinatesToTile(tlx, tly, map.zoom, out ttlx, out ttly); map.projection.CoordinatesToTile(brx, bry, map.zoom, out tbrx, out tbry); if (tbrx < ttlx) { tbrx += 1 << map.zoom; } double cx = (tbrx - ttlx) * x + ttlx; double cz = (tbry - ttly) * z + ttly; int zoom = map.zoom - zoomOffset; double tx, ty; map.projection.TileToCoordinates(cx, cz, map.zoom, out cx, out cz); map.projection.CoordinatesToTile(cx, cz, zoom, out tx, out ty); int ix = (int)tx; int iy = (int)ty; ulong key = OnlineMapsTileManager.GetTileKey(zoom, ix, iy); Tile tile; bool hasTile = tiles.TryGetValue(key, out tile); if (hasTile && !tile.loaded) { hasTile = false; } if (!hasTile) { int nz = zoom; while (!hasTile && nz < OnlineMaps.MAXZOOM) { nz++; map.projection.CoordinatesToTile(cx, cz, nz, out tx, out ty); ix = (int)tx; iy = (int)ty; key = OnlineMapsTileManager.GetTileKey(nz, ix, iy); hasTile = tiles.TryGetValue(key, out tile) && tile.loaded; } } if (!hasTile) { int nz = zoom; while (!hasTile && nz > 1) { nz--; map.projection.CoordinatesToTile(cx, cz, nz, out tx, out ty); ix = (int)tx; iy = (int)ty; key = OnlineMapsTileManager.GetTileKey(nz, ix, iy); hasTile = tiles.TryGetValue(key, out tile) && tile.loaded; } } if (!hasTile) { return(0); } map.projection.CoordinatesToTile(cx, cz, tile.zoom, out tx, out ty); return(tile.GetElevation(tx, ty)); }
public void StartDownloadTile(OnlineMapsTile tile) { OnlineMapsTileManager.StartDownloadTile(tile); }
public string GetCacheKey(string prefix) { return(prefix + OnlineMapsTileManager.GetTileKey(zoom, x, y)); }
public static ulong GetTileKey(int zoom, int x, int y) { return(OnlineMapsTileManager.GetTileKey(zoom, x, y)); }
private void PreloadBorders() { int zoom = map.zoom - zoomOffset; if (zoom < 3) { zoom = 3; } double tx, ty; map.GetTilePosition(out tx, out ty, zoom); int itx = Mathf.RoundToInt((float)(tx - countX / 2f)); int ity = Mathf.RoundToInt((float)(ty - countY / 2f)); foreach (OnlineMapsTile tile in extraTiles) { tile.Unblock(this); } extraTiles.Clear(); int max = 1 << zoom; OnlineMapsTileManager tileManager = map.tileManager; for (int x = -1; x < countX + 1; x++) { int tileX = itx + x; if (tileX >= max) { tileX -= max; } int tileY = ity - 1; if (tileY >= max) { tileY -= max; } OnlineMapsTile tile = tileManager.GetTile(zoom, tileX, tileY); if (tile == null) { OnlineMapsTile parentTile = tileManager.GetTile(zoom - 1, tileX / 2, tileY / 2); tile = new OnlineMapsRasterTile(tileX, tileY, zoom, map); tile.parent = parentTile; } tile.Block(this); extraTiles.Add(tile); tileY = ity + countY; if (tileY >= max) { tileY -= max; } tile = tileManager.GetTile(zoom, tileX, tileY); if (tile == null) { OnlineMapsTile parentTile = tileManager.GetTile(zoom - 1, tileX / 2, tileY / 2); tile = new OnlineMapsRasterTile(tileX, tileY, zoom, map); tile.parent = parentTile; } tile.Block(this); extraTiles.Add(tile); } for (int y = 0; y < countY; y++) { int tileY = ity + y; if (tileY >= max) { tileY -= max; } int tileX = itx - 1; if (tileX >= max) { tileX -= max; } OnlineMapsTile tile = tileManager.GetTile(zoom, tileX, tileY); if (tile == null) { OnlineMapsTile parentTile = tileManager.GetTile(zoom - 1, tileX / 2, tileY / 2); tile = new OnlineMapsRasterTile(tileX, tileY, zoom, map); tile.parent = parentTile; } tile.Block(this); extraTiles.Add(tile); tileX = itx + countX; if (tileX >= max) { tileX -= max; } tile = tileManager.GetTile(zoom, tileX, tileY); if (tile == null) { OnlineMapsTile parentTile = tileManager.GetTile(zoom - 1, tileX / 2, tileY / 2); tile = new OnlineMapsRasterTile(tileX, tileY, zoom, map); tile.parent = parentTile; } tile.Block(this); extraTiles.Add(tile); } }
private void OnLateUpdateBefore() { if (!needUpdateTiles) { if (needUpdateMinMax) { UpdateMinMax(); } return; } needUpdateTiles = false; int zoom = map.zoom - zoomOffset; if (zoom < 1) { zoom = 1; } if (!zoomRange.InRange(map.zoom)) { return; } int currentOffset = map.zoom - zoom; int coef = (1 << currentOffset) * OnlineMapsUtils.tileSize; int countX = Mathf.CeilToInt(map.width / 2f / coef) * 2 + 2; int countY = Mathf.CeilToInt(map.height / 2f / coef) * 2 + 2; double sx, sy; map.GetTilePosition(out sx, out sy, zoom); prevTileX = (int)sx; prevTileY = (int)sy; int isx = prevTileX - countX / 2; int isy = prevTileY - countY / 2; int max = 1 << zoom; foreach (KeyValuePair <ulong, Tile> pair in tiles) { pair.Value.used = false; } for (int x = isx; x < isx + countX; x++) { int cx = x; if (cx < 0) { cx += max; } else if (cx >= max) { cx -= max; } for (int y = Mathf.Max(isy, 0); y < Mathf.Min(isy + countY, max); y++) { ulong key = OnlineMapsTileManager.GetTileKey(zoom, cx, y); Tile t; if (tiles.TryGetValue(key, out t)) { t.used = true; continue; } t = new Tile { x = x, y = y, zoom = zoom, width = tileWidth, height = tileHeight, used = true }; tiles.Add(key, t); if (OnDownload != null) { OnDownload(t); } else { StartDownloadElevationTile(t); } } } double tlx, tly, brx, bry; map.GetTileCorners(out tlx, out tly, out brx, out bry); int itlx = (int)tlx; int itly = (int)tly; int ibrx = (int)brx; int ibry = (int)bry; List <ulong> unloadKeys = new List <ulong>(); foreach (KeyValuePair <ulong, Tile> pair in tiles) { Tile tile = pair.Value; if (tile.used) { continue; } int scale = 1 << (map.zoom - tile.zoom); int tx = tile.x * scale; int ty = tile.y * scale; if (ibrx >= tx && itlx <= tx + scale && ibry >= ty && itly <= ty + scale) { if (Mathf.Abs(zoom - tile.zoom) < 3) { continue; } } unloadKeys.Add(pair.Key); } foreach (ulong key in unloadKeys) { tiles.Remove(key); } UpdateMinMax(); }