#if !UNITY_WEBPLAYER using System; using System.Collections.Generic; using System.IO; using UnityEngine; public class CacheAreaExample: MonoBehaviour { const int maxDownloadCount = 3; private static List tilesForCache = new List(); private static List activeTiles = new List(); public static void Cache(Vector2 topLeftCoordinates, Vector2 bottomRightCoordinates, int minZoom = 10, int maxZoom = 15) { minZoom = Mathf.Clamp(minZoom, 3, 20); maxZoom = Mathf.Clamp(maxZoom, 3, 20); if (minZoom > maxZoom) maxZoom = minZoom; for (int zoom = minZoom; zoom <= maxZoom; zoom++) { OnlineMapsVector2i topLeft = OnlineMapsUtils.LatLongToTile(topLeftCoordinates, zoom); OnlineMapsVector2i bottomRight = OnlineMapsUtils.LatLongToTile(bottomRightCoordinates, zoom) + new OnlineMapsVector2i(1, 1); int maxX = 1 << zoom; if (bottomRight.x < topLeft.x) bottomRight.x += maxX; for (int x = topLeft.x; x < bottomRight.x; x++) { int cx = x; if (cx >= maxX) cx -= maxX; for (int y = topLeft.y; y < bottomRight.y; y++) { OnlineMapsTile tile = new OnlineMapsTile(cx, y, zoom, OnlineMaps.instance, false); string tilePath = GetTilePath(tile); if (!File.Exists(tilePath)) tilesForCache.Add(tile); } } } } private void CheckDownloadComplete() { foreach (OnlineMapsTile tile in activeTiles) { if (tile.www != null && tile.www.isDone) { string path = GetTilePath(tile); FileInfo fileInfo = new FileInfo(path); DirectoryInfo directory = fileInfo.Directory; if (!directory.Exists) directory.Create(); File.WriteAllBytes(path, tile.www.bytes); tile.www = null; } } activeTiles.RemoveAll(t => t.www == null); } /// /// Gets the local path for tile. /// /// Reference to tile /// Local path for tile private static string GetTilePath(OnlineMapsTile tile) { string[] parts = { Application.persistentDataPath, "OnlineMapsTileCache", Enum.GetName(typeof(OnlineMapsProviderEnum), tile.provider), OnlineMaps.instance.type.ToString(), tile.zoom.ToString(), tile.x.ToString(), tile.y + ".png" }; return string.Join("/", parts); } private void OnGUI() { if (GUI.Button(new Rect(5, 5, 100, 30), "Cache area")) { OnlineMaps api = OnlineMaps.instance; Cache(api.topLeftPosition, api.bottomRightPosition, api.zoom, api.zoom + 3); } } /// /// This method is called when loading the tile. /// /// Reference to tile 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); tileTexture.LoadImage(File.ReadAllBytes(path)); tileTexture.wrapMode = TextureWrapMode.Clamp; // Send texture to map. if (OnlineMaps.instance.target == OnlineMapsTarget.texture) { tile.ApplyTexture(tileTexture); OnlineMaps.instance.buffer.ApplyTile(tile); } 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. OnlineMaps.instance.StartDownloadTile(tile); } } private void Start() { // Intercepts requests to the download of the tile. OnlineMaps.instance.OnStartDownloadTile += OnStartDownloadTile; } private void Update() { if (tilesForCache == null) tilesForCache = new List(); if (activeTiles == null) activeTiles = new List(); CheckDownloadComplete(); while (activeTiles.Count < maxDownloadCount && tilesForCache.Count > 0) { OnlineMapsTile tile = tilesForCache[0]; tilesForCache.RemoveAt(0); activeTiles.Add(tile); tile.www = new WWW(tile.url); } } } #endif