示例#1
0
        internal IEnumerator LoadTileContentBackground(TileInfo ti)
        {
            yield return(new WaitForEndOfFrame());

            string url = GetTileURL(_tileServer, ti);

            if (string.IsNullOrEmpty(url))
            {
                Debug.LogError("Tile server url not set. Aborting");
                yield break;
            }

            long   downloadedBytes = 0;
            string error           = null;
            string filePath        = "";

            byte[] textureBytes = null;
            ti.source = TILE_SOURCE.Unknown;

            // Check if tile is given by external event
            if (OnTileRequest != null)
            {
                if (OnTileRequest(ti.zoomLevel, ti.x, ti.y, out ti.texture, out error) && ti.texture != null)
                {
                    ti.source = TILE_SOURCE.Resources;
                }
            }

            // Check if tile is in Resources
            if (ti.source == TILE_SOURCE.Unknown && _tileEnableOfflineTiles)
            {
                string          path    = GetTileResourcePath(ti.x, ti.y, ti.zoomLevel, false);
                ResourceRequest request = Resources.LoadAsync <Texture2D> (path);
                yield return(request);

                if (request.asset != null)
                {
                    ti.texture = (Texture2D)request.asset;
                    ti.source  = TILE_SOURCE.Resources;
                }
                else if (tileOfflineTilesOnly)
                {
                    ti.texture = tileResourceFallbackTexture;
                    ti.source  = TILE_SOURCE.Resources;
                }
            }

            CustomWWW www = null;

            if (ti.source == TILE_SOURCE.Unknown)
            {
                www = getCachedWWW(url, ti);
                yield return(www);

                error = www.error;
            }

            for (int tries = 0; tries < 100; tries++)
            {
                if (spreadLoadAmongFrames > 0)
                {
                    break;
                }
                yield return(new WaitForEndOfFrame());
            }
            spreadLoadAmongFrames--;
            _concurrentLoads--;

            if (!ti.visible)                    // non visible textures are ignored
            {
                ti.loadStatus = TILE_LOAD_STATUS.InQueue;
                yield break;
            }

            if (!string.IsNullOrEmpty(error))
            {
                _tileLastError     = "Error getting tile: " + error + " url=" + url;
                _tileLastErrorDate = DateTime.Now;
                if (_tileDebugErrors)
                {
                    Debug.Log(_tileLastErrorDate + " " + _tileLastError);
                }
                ti.loadStatus = TILE_LOAD_STATUS.InQueue;
                yield break;
            }

            // Load texture
            if (ti.source != TILE_SOURCE.Resources)
            {
                downloadedBytes = www.bytesDownloaded;
                textureBytes    = www.bytes;
                ti.texture      = www.textureNonReadable;
                www.Dispose();
                www = null;

                // Check texture consistency
                if (ti.loadedFromCache || _tileEnableLocalCache)
                {
                    filePath = GetLocalFilePathForURL(url, ti);
                }

                if (ti.loadedFromCache && ti.texture.width <= 16)                   // Invalid texture in local cache, retry
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                    ti.loadStatus = TILE_LOAD_STATUS.Inactive;
                    ti.queueTime  = Time.time;
                    yield break;
                }
            }

            ti.texture.wrapMode = TextureWrapMode.Clamp;
            _tileSize           = ti.texture.width;

            // Save texture
            if (_tileEnableLocalCache && ti.source != TILE_SOURCE.Resources && !File.Exists(filePath))
            {
                _tileCurrentCacheUsage += textureBytes.Length;
                BackgroundSaver saver = new BackgroundSaver(textureBytes, filePath);
                saver.Start();
            }

            // Update stats
            switch (ti.source)
            {
            case TILE_SOURCE.Cache:
                _cacheLoads++;
                _cacheLoadTotalSize += downloadedBytes;
                break;

            case TILE_SOURCE.Resources:
                _resourceLoads++;
                break;

            default:
                _webDownloads++;
                _webDownloadTotalSize += downloadedBytes;
                break;
            }

            if (loadQueue.Contains(ti))
            {
                loadQueue.Remove(ti);
            }

            FinishLoadingTile(ti);
        }
示例#2
0
        internal IEnumerator LoadTileContentBackground(TileInfo ti)
        {
            yield return(new WaitForEndOfFrame());

            string url;

            GetTileServerInfo(_tileServer, ti, out url);
            if (url == null)
            {
                Debug.LogError("Tile server url not set. Aborting");
                yield break;
            }

            if (tileServerAPIKey != null && tileServerAPIKey.Length > 0)
            {
                url += "?" + _tileServerAPIKey;
            }

            WWW www = getCachedWWW(url, ti);

            yield return(www);

            _concurrentLoads--;

            if (www.error != null)
            {
                _tileLastError     = "Error getting tile: " + www.error + " url=" + url;
                _tileLastErrorDate = DateTime.Now;
                if (_tileDebugErrors)
                {
                    Debug.Log(_tileLastErrorDate + " " + _tileLastError);
                }
                ti.loadStatus = TILE_LOAD_STATUS.InQueue;
                yield break;
            }
            // Check texture consistency
            string filePath = GetLocalFilePathForUrl(url, ti);

            long downloadedBytes = www.size;

            ti.texture = new Texture2D(www.texture.width, www.texture.height);
            www.LoadImageIntoTexture(ti.texture);
            www.Dispose();
            www = null;

            if (ti.texture.width <= 16 && ti.loadedFromCache)               // Invalid texture in local cache, retry
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
                ti.loadStatus = TILE_LOAD_STATUS.Inactive;
                ti.queueTime  = Time.time;
                yield break;
            }
            ti.texture.wrapMode = TextureWrapMode.Clamp;
            _tileSize           = ti.texture.width;

            // Save texture
            if (!File.Exists(filePath))
            {
                byte[] texBytes = ti.texture.EncodeToJPG();
                _tileCurrentCacheUsage += texBytes.Length;
                BackgroundSaver saver = new BackgroundSaver(texBytes, filePath);
                saver.Start();
            }

            // Update stats
            if (ti.loadedFromCache)
            {
                _cacheLoads++;
                _cacheLoadTotalSize += downloadedBytes;
            }
            else
            {
                _webDownloads++;
                _webDownloadTotalSize += downloadedBytes;
            }

            // Good to go, update tile info
            switch (ti.subquadIndex)
            {
            case 0:
                ti.parent.transMat.SetTexture("_MainTex", ti.texture);
                ti.parent.normalMat.SetTexture("_MainTex", ti.texture);
                break;

            case 1:
                ti.parent.transMat.SetTexture("_MainTex1", ti.texture);
                ti.parent.normalMat.SetTexture("_MainTex1", ti.texture);
                break;

            case 2:
                ti.parent.transMat.SetTexture("_MainTex2", ti.texture);
                ti.parent.normalMat.SetTexture("_MainTex2", ti.texture);
                break;

            case 3:
                ti.parent.transMat.SetTexture("_MainTex3", ti.texture);
                ti.parent.normalMat.SetTexture("_MainTex3", ti.texture);
                break;
            }
            ti.loadStatus = TILE_LOAD_STATUS.Loaded;
            if (loadQueue.Contains(ti))
            {
                loadQueue.Remove(ti);
            }

            if (ti.zoomLevel >= TILE_MIN_ZOOM_LEVEL)
            {
                if (ti.y == 0 || ti.y == zoomLevelsInfo [ti.zoomLevel].yMax - 1)
                {
                    CreatePole(ti);
                }
            }
            shouldCheckTiles = true;
        }