public static UnityWebRequest LoadFromCacheOrDownload(string url, out bool disposable) { disposable = true; if (url.StartsWith("jar:") == true) { return(UnityWebRequest.Get(url)); } var uri = new Uri(); if (!uri.Parse(url)) { EB.Debug.LogError("LoadFromCacheOrDownload Parse Failed {0}", url); return(null); } if (uri.Scheme == "file") { return(UnityWebRequest.Get(url)); } // assume network var fileName = GetCacheFile(url); if (File.Exists(fileName)) { var cachedPath = "file://" + fileName; return(UnityWebRequest.Get(cachedPath)); } // load it and save it to the cache var www = UnityWebRequest.Get(url); disposable = false; EB.Debug.Log("Loading to Cache {0}", url); _ActivelyLoading.Add(www); Coroutines.Run(SaveToCache(url, www)); return(www); }
/// <summary> /// 异步加载Texture /// </summary> /// <param name="info"></param> /// <returns></returns> IEnumerator ProcessNextTexture(TextureInfo info) { string url = GetUrl(info.textureName); // If we didn't find an url and our textures are prepended with our // Bundles/UI/ path, strip it and look for the path again if (string.IsNullOrEmpty(url)) { url = GetUrl(info.textureName.Replace("Bundles/UI/", string.Empty)); } #if TEXTUREPOOL_SPEW EB.Debug.Log("TexturePoolManager - LOADING " + url); #endif // evict any old textures before moving on Evict(); UnityWebRequest textureLoader = null; bool disposable = true; EB.Uri uri = new EB.Uri(); if (uri.Parse(url)) { for (int i = 0; i < kRetryIntervals; ++i) { textureLoader = EB.Cache.LoadFromCacheOrDownload(url, out disposable); if (textureLoader == null) { break; } yield return(textureLoader.SendWebRequest()); if (string.IsNullOrEmpty(textureLoader.error)) { break; } else if (url.StartsWith("file://")) { // file error are going to make a difference with a retry break; } #if UNITY_EDITOR // Since all the texture are in Resources folder when running in editor, we don't want to delay this if (Application.isEditor) { break; } #else EB.Debug.LogWarning("COULD NOT FIND TEXTURE: " + url); yield return(_waits[i]); #endif } } if (Application.isEditor) { yield return(new WaitForSeconds(0.15f)); } if (textureLoader != null && (textureLoader.isHttpError || textureLoader.isNetworkError)) { // If we loaded from cache or file, we want to mark the textures properly // so they are destroyed or released properly later on if (textureLoader.url.StartsWith("file://")) { info.source = TextureInfo.eTextureSource.Resources; } info.texture = DownloadHandlerTexture.GetContent(textureLoader); info.texture.wrapMode = TextureWrapMode.Clamp; info.texture.name = info.textureName; info.successfullyLoaded = true; info.DoCallbacks(); #if TEXTUREPOOL_SPEW EB.Debug.LogWarning("ON-DEMAND LOAD FROM {0} THERE ARE NOW {1} IN THE POOL", url, _all.Count); #endif } else { // Texture wasn't found in the bundle - Look for it in the resources folder only if in editor // Resources load requires no extension present in the path name string strippedPath = info.textureName.Replace(System.IO.Path.GetExtension(info.textureName), string.Empty); info.texture = Resources.Load(strippedPath) as Texture2D; if (info.texture == null) { EB.Debug.LogWarning("Texture not found at '{0}' or '{1}'", info.textureName, strippedPath); } else { info.texture.wrapMode = TextureWrapMode.Clamp; info.texture.name = info.textureName; info.source = TextureInfo.eTextureSource.Resources; info.successfullyLoaded = true; } info.DoCallbacks(); } if (textureLoader != null) { if (disposable == true) { textureLoader.Dispose(); } else { EB.Cache.DisposeOnComplete(textureLoader); } } --_concurrentRequests; }