示例#1
0
 private static void DownloadFallbackOrUseErrorImage(string fallbackImageUrl, OnImageDownloaded onImageDownload)
 {
     if (string.IsNullOrEmpty(fallbackImageUrl))
     {
         Texture2D image = Resources.Load("no_image", typeof(Texture2D)) as Texture2D;
         onImageDownload(image);
     }
     else
     {
         DownloadImage(fallbackImageUrl, 0, onImageDownload, string.Empty);
     }
 }
示例#2
0
        IEnumerator LoadImageAndWaitCoroutine(string url, OnImageDownloaded onImageDownloaded)
        {
            var filename = WWW.EscapeURL(url);
            var filePath = CacheFolder + filename;

            //记录时间戳
            if (ImageTimeStampDict.ContainsKey(filename))
            {
                ImageTimeStampDict[filename] = DateTime.Now.Ticks;
            }
            else
            {
                ImageTimeStampDict.Add(filename, DateTime.Now.Ticks);
            }
            _flushAndReleaseNow = true;

            var dirInfo = new DirectoryInfo(CacheFolder);

            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }

            if (File.Exists(filePath))
            {
                if (onImageDownloaded != null)
                {
                    var bytes   = new byte[1];// File.ReadAllBytes(filePath);
                    var texture = new Texture2D(1, 1);
                    texture.LoadImage(bytes);
                    onImageDownloaded(texture);
                }
            }
            else
            {
                var www = new WWW(url);
                yield return(www);

                if (www.error == null)
                {
                    Debug.Log("下载图片,大小(B):" + www.size);
                    //File.WriteAllBytes(filePath, www.bytes);
                    if (onImageDownloaded != null)
                    {
                        onImageDownloaded(www.texture);
                    }
                }
                else
                {
                    Debug.LogError(www.error);
                }
            }
        }
示例#3
0
    public static void DownloadImage(string imageUrl, int imageSize, OnImageDownloaded onImageDownload, string fallbackImageUrl = "", bool isRetry = false)
    {
        ImageDownloader imageDownloaderQueue = CreateInstanceIfNeeded();

        if (!string.IsNullOrEmpty(imageUrl))
        {
            if (downloadingImages == null)
            {
                downloadingImages = new Dictionary <string, List <OnImageDownloaded> >();
            }
            if (downloadedImages == null)
            {
                downloadedImages = new Dictionary <string, Texture2D>();
            }
            if (cachedImageQueue == null)
            {
                cachedImageQueue = new List <string>();
            }
            string cacheRef = imageUrl + ":" + imageSize;
            if (downloadedImages.ContainsKey(cacheRef))
            {
                onImageDownload(downloadedImages[cacheRef]);
            }
            else if (downloadingImages.ContainsKey(cacheRef))
            {
                downloadingImages[cacheRef].Add(onImageDownload);
            }
            else
            {
                try
                {
                    string url  = imageUrl;
                    bool   flag = false;
                    if (imageSize != 0 && imageUrl.StartsWith("https://api.vrchat.cloud/api/1/file/"))
                    {
                        string[] array = imageUrl.Remove(0, "https://api.vrchat.cloud/api/1/file/".Length).Split('/');
                        if (array.Length == 2 || (array.Length == 3 && array[2] == "file"))
                        {
                            string text  = array[0];
                            string text2 = array[1];
                            url  = "https://api.vrchat.cloud/api/1/image/" + text + "/" + text2 + "/" + imageSize.ToString();
                            flag = true;
                        }
                    }
                    downloadingImages[cacheRef] = new List <OnImageDownloaded>();
                    HTTPManager.SendRequest(url, HTTPMethods.Get, HTTPManager.KeepAliveDefaultValue, disableCache : false, delegate(HTTPRequest request, HTTPResponse response)
                    {
                        Action loadImage2 = delegate
                        {
                            if (response != null)
                            {
                                Texture2D dataAsTexture2D = response.DataAsTexture2D;
                                EncacheTexture(cacheRef, dataAsTexture2D);
                                onImageDownload(dataAsTexture2D);
                                foreach (OnImageDownloaded item in downloadingImages[cacheRef])
                                {
                                    item(response.DataAsTexture2D);
                                }
                                downloadingImages.Remove(cacheRef);
                            }
                            else
                            {
                                Debug.LogError((object)("No response received: " + ((request.Exception == null) ? "No Exception" : (request.Exception.Message + "\n" + request.Exception.StackTrace))));
                                DownloadFallbackOrUseErrorImage(fallbackImageUrl, onImageDownload);
                            }
                        };
                        if (response != null && response.Data == null)
                        {
                            downloadingImages.Remove(cacheRef);
                            HTTPCacheService.DeleteEntity(request.CurrentUri);
                            if (!isRetry)
                            {
                                DownloadImage(imageUrl, imageSize, onImageDownload, fallbackImageUrl, isRetry: true);
                            }
                        }
                        else if (imageDownloaderQueue != null)
                        {
                            imageDownloaderQueue.QueueImageLoad(loadImage2);
                        }
                    });
                }
                catch (Exception ex)
                {
                    Exception e;
                    Exception ex2       = e = ex;
                    Action    loadImage = delegate
                    {
                        Debug.Log((object)("Could not download image " + imageUrl + " - " + e.Message));
                        DownloadFallbackOrUseErrorImage(fallbackImageUrl, onImageDownload);
                    };
                    imageDownloaderQueue.QueueImageLoad(loadImage);
                }
            }
        }
    }
示例#4
0
 public static void LoadImageAndWait(string url, OnImageDownloaded onImageDownloaded)
 {
     Instance.StartCoroutine(Instance.LoadImageAndWaitCoroutine(url, onImageDownloaded));
     Instance._flushAndReleaseNow = true;
 }