示例#1
0
            /// <summary>
            /// The download the coroutine.
            /// </summary>
            /// <returns>The coroutine.</returns>
            private IEnumerator DownloadCoroutine()
            {
                WWW www = null;

                bool cacheHit = this.Cached;

                if (cacheHit)
                {
                    www = new WWW("file:///" + this.cacheFileName);
                }
                else
                {
                    www = new WWW(url);
                    // Debug.Log(url);
                }

                yield return(www);

                if (String.IsNullOrEmpty(www.error) && www.text.Contains("404 Not Found") == false)
                {
                    Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, true);
                    www.LoadImageIntoTexture(texture);

                    if (cacheHit == false)
                    {
                        byte[] bytes = www.bytes;

                        FileStream fs = new FileStream(this.cacheFileName, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true);
                        fs.BeginWrite(bytes, 0, bytes.Length, new AsyncCallback(EndWriteCallback), new AsyncInfo(this, fs));
                    }

                    tile.SetTexture(texture);
                }
                else
                {
                    this.error = true;
                }
            }
		/// <summary>
		/// Requests the tile's texture and assign it.
		/// </summary>
		/// <param name="tileX">Tile x.</param>
		/// <param name="tileY">Tile y.</param>
		/// <param name="roundedZoom">Rounded zoom.</param>
		/// <param name="tile">Tile.</param>
		protected override void RequestTile (int tileX, int tileY, int roundedZoom, TileBehaviour tile)
		{
			if (db == null) {
				throw new NullReferenceException ("db");
			}
		
			DataTable dt = db.ExecuteQuery ("SELECT tile_data FROM tiles WHERE zoom_level=" + roundedZoom + " AND tile_column=" + tileX + " AND tile_row=" + tileY);
			if (dt.Rows.Count == 0) {
#if DEBUG_LOG
			Debug.LogWarning("WARNING: no rows in MBTiles db for tile: " + tileX + "," + tileY + "," + roundedZoom);
#endif
				return;
			}
		
			Texture2D tex = new Texture2D ((int)Map.TileResolution, (int)Map.TileResolution);
			if (tex.LoadImage ((byte[])dt.Rows [0] ["tile_data"]))
				tile.SetTexture (tex);
			else {
#if DEBUG_LOG
			Debug.LogError("ERROR: MBTilesLayer.RequestTile: couldn't load image for: " + tileX + "," + tileY + "," + roundedZoom);
#endif
			}
		}
            /// <summary>
            /// The download the coroutine.
            /// </summary>
            /// <returns>The coroutine.</returns>
            private IEnumerator DownloadCoroutine()
            {
                WWW    www = null;
                string ext = Path.GetExtension(url);

                if (ext.Contains("?"))
                {
                    ext = ext.Substring(0, ext.IndexOf('?'));
                }

                if (cached && File.Exists(Application.temporaryCachePath + "/" + this.guid + ext))
                {
                    www = new WWW("file:///" + Application.temporaryCachePath + "/" + this.guid + ext);
#if DEBUG_LOG
                    Debug.Log("DEBUG: TileDownloader.DownloadCoroutine: loading tile from cache: url: " + www.url);
#endif
                }
                else
                {
                    www = new WWW(url);
#if DEBUG_LOG
                    Debug.Log("DEBUG: TileDownloader.DownloadCoroutine: loading tile from provider: url: " + www.url
                              + "(cached: " + cached + ")"
                              );
#endif
                }

                yield return(www);

#if DEBUG_PROFILE
                UnitySlippyMap.Profiler.Begin("TileDownloader.TileEntry.DownloadCoroutine");
#endif

#if DEBUG_PROFILE
                UnitySlippyMap.Profiler.Begin("www error test");
#endif
                if (String.IsNullOrEmpty(www.error) && www.text.Contains("404 Not Found") == false)
                {
#if DEBUG_PROFILE
                    UnitySlippyMap.Profiler.End("www error test");
#endif
#if DEBUG_PROFILE
                    UnitySlippyMap.Profiler.Begin("www.texture");
#endif

                    Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, true);
                    www.LoadImageIntoTexture(texture);

#if DEBUG_PROFILE
                    UnitySlippyMap.Profiler.End("www.texture");
#endif

#if DEBUG_PROFILE
                    UnitySlippyMap.Profiler.Begin("is cached?");
#endif

                    if (this.cached == false)
                    {
#if DEBUG_PROFILE
                        UnitySlippyMap.Profiler.End("is cached?");
#endif

#if DEBUG_PROFILE
                        UnitySlippyMap.Profiler.Begin("set TileEntry members");
#endif

                        byte[] bytes = www.bytes;

                        this.size = bytes.Length;
                        this.guid = Guid.NewGuid().ToString();
#if DEBUG_PROFILE
                        UnitySlippyMap.Profiler.End("set TileEntry members");
#endif

#if DEBUG_PROFILE
                        UnitySlippyMap.Profiler.Begin("new FileStream & FileStream.BeginWrite");
#endif
                        FileStream fs = new FileStream(Application.temporaryCachePath + "/" + this.guid + ext, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true);
                        fs.BeginWrite(bytes, 0, bytes.Length, new AsyncCallback(EndWriteCallback), new AsyncInfo(this, fs));
#if DEBUG_PROFILE
                        UnitySlippyMap.Profiler.End("new FileStream & FileStream.BeginWrite");
#endif

#if DEBUG_LOG
                        Debug.Log("DEBUG: TileEntry.DownloadCoroutine: done loading: " + www.url + ", writing to cache: " + fs.Name);
#endif
                    }
                    else
                    {
#if DEBUG_PROFILE
                        UnitySlippyMap.Profiler.End("is cached?");
#endif
#if DEBUG_LOG
                        Debug.Log("DEBUG: TileEntry.DownloadCoroutine: done loading from cache: " + www.url + " [" + url + "]");
#endif
                    }

                    this.timestamp = (DateTime.Now - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;

#if DEBUG_PROFILE
                    UnitySlippyMap.Profiler.Begin("Tile.SetTexture");
#endif
                    tile.SetTexture(texture);
#if DEBUG_PROFILE
                    UnitySlippyMap.Profiler.End("Tile.SetTexture");
#endif
                }
                else
                {
#if DEBUG_PROFILE
                    UnitySlippyMap.Profiler.End("www error test");
#endif
                    this.error = true;
#if DEBUG_LOG
                    Debug.LogError("ERROR: TileEntry.DownloadCoroutine: done downloading: " + www.url + " with error: " + www.error);
#endif
                }

#if DEBUG_PROFILE
                UnitySlippyMap.Profiler.End("TileDownloader.TileEntry.DownloadCoroutine");
#endif
            }