void Download() { // Manage downloads int activeDownloads = 0; for (int k = 0; k < concurrentDownloads; k++) { if (downloads [k].busy) { CustomWWW www = downloads [k].www; if (www.isDone) { bool good = string.IsNullOrEmpty(www.error); // Check texture consistency if (good) { Texture2D tex = www.textureNonReadable; if (tex == null) { good = false; } else if (tex.width < 16) { good = false; DestroyImmediate(tex); } } if (good) { // save to resources bytesDownloaded += www.bytes.Length; File.WriteAllBytes(downloads [k].path, www.bytes); downloadedTilesCount++; downloadedTilesSize += bytesDownloaded; downloads [k].busy = false; } else { if (downloads [k].retries < 3) { downloads [k].retries++; Debug.LogError("Retrying " + downloads [k].retries + " times due error on tile XYZ = " + downloads [k].x + "/" + downloads [k].y + "/" + downloads [k].zoomLevel + " : " + www.error); downloads [k].www = new CustomWWW(www.url); activeDownloads++; } else { downloads [k].busy = false; } } www.Dispose(); } else { activeDownloads++; } } } int iterations = 0; for (int k = 0; k < concurrentDownloads; k++) { if (!downloads [k].busy) { if (activeDownloads >= concurrentDownloads) { return; } // Get next tile bool remainingTiles = GetNextTile(); // Have we finished? if (!remainingTiles) { if (activeDownloads == 0) { StopOperation(); } return; } ti.x = x; ti.y = y; ti.zoomLevel = zoomLevel; string url = map.GetTileURL(map.tileServer, ti); // Is current tile already in Resources path? string tilePath = map.GetTileResourcePath(x, y, zoomLevel); if (File.Exists(tilePath)) { if (++iterations < 128) { k--; } } else { downloads [k].busy = true; downloads [k].retries = 0; downloads [k].x = x; downloads [k].y = y; downloads [k].zoomLevel = zoomLevel; downloads [k].path = tilePath; downloads [k].www = new CustomWWW(url); activeDownloads++; } } } }