public Texture2D GetTextureFromZoomAndCoords(int zoom, int coordX, int coordY) { Texture2D texture = null; string key = MapGeneratorOSM.GetKeyFromZoomAndCoords(zoom, coordX, coordY); string tilePath = GetTilePathFromZoomAndCoords(zoom, coordX, coordY); bool fileExists = false; lock (fileAccessLock) { fileExists = File.Exists(tilePath); } if (!allTileTexturesFilePathDico.ContainsKey(key) && fileExists) { // texture filename is not known but file already exist in memory ! // memorize filename allTileTexturesFilePathDico.Add(key, tilePath); } if (allTileTexturesDico.ContainsKey(key)) { // texture already exists ! Just return it texture = allTileTexturesDico[key]; } else if (allTileTexturesFilePathDico.ContainsKey(key) && fileExists) { // texture filename is already known and the corresponding file exists, create corresponding texture and save it texture = LoadPNG(allTileTexturesFilePathDico[key]); allTileTexturesDico.Add(key, texture); } else { //Debug.LogWarning("No texture nor filepath nor file exist !"); } return(texture); }
public static void Initialize(MapGeneratorOSM generator) { instance = new TileLoader(); webRequestCount = 0; instance.mapGenerator = generator; }
/// <summary> /// Load tile either from local cache or from web (in this case, send an asynchronous request that will be received later) /// Returns true if load happened (either from cache or from web), returns false if it didn't (because too many requests are pending) /// </summary> /// <param name="zoom"></param> /// <param name="coordX"></param> /// <param name="coordY"></param> /// <returns></returns> public bool LoadTileImageFromZoomAndCoords(int zoom, int coordX, int coordY) { string key = MapGeneratorOSM.GetKeyFromZoomAndCoords(zoom, coordX, coordY); string tilePath = GetTilePathFromZoomAndCoords(zoom, coordX, coordY); if (!allTileTexturesFilePathDico.ContainsKey(key)) { // We don't know tile path, it means we never created the file nor made a request to download it bool fileExists = false; lock (fileAccessLock) { fileExists = File.Exists(tilePath); if (fileExists) { // check if file really contains something (never too careful) FileInfo info = new FileInfo(tilePath); if (info.Length <= 0) { // file exist but is empty fileExists = false; } } } if (fileExists) { // but it already exists in cache ! So we should just ask for rendering mapGenerator.EnqueueTileToDisplay(key); } else { // it doesn't exist yet, so we should create it and download its content CreateTileDirectoriesIfNeeded(zoom, coordX); // except if there are too many requests pending if (webRequestCount >= webRequestMaxCount) { //Debug.LogWarning("Prevent download of tile : " + key + " because webRequestCount is = " + webRequestCount); return(false); } allTileTexturesFilePathDico.Add(key, tilePath); // Call web request to download image lock (fileAccessLock) { try { string url = RandomServer() + "/" + zoom + "/" + coordX + "/" + coordY + ".png"; //Debug.Log(url); WebClient client = new WebClient(); // sync //client.DownloadFile(new Uri(url), tilePath); // async client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadDataCallback); client.DownloadFileAsync(new Uri(url), tilePath, key); webRequestCount++; } catch (System.Exception ex) { Debug.LogError("LoadTileImageFromZoomAndCoords got exception : " + ex.Message); } } } } return(true); }
void Awake() { MapGeneratorOSM.instance = this; }