// Make room for image if needed (because of cache limits)
 private static void ExpireCacheEntries()
 {
     while (CacheFull)
     {
         string oldestUrl = null;
         CachedAssetStoreImage oldestEntry = null;
         foreach (KeyValuePair <string, CachedAssetStoreImage> kv in CachedAssetStoreImages)
         {
             if (oldestEntry == null || oldestEntry.lastUsed > kv.Value.lastUsed)
             {
                 oldestEntry = kv.Value;
                 oldestUrl   = kv.Key;
             }
         }
         CachedAssetStoreImages.Remove(oldestUrl);
         Instance.m_CacheRemove++;
         if (oldestEntry == null)
         {
             Debug.LogError("Null entry found while removing cache entry");
             break;
         }
         if (oldestEntry.client != null)
         {
             oldestEntry.client.Abort();
             oldestEntry.client = null;
         }
         if (oldestEntry.image != null)
         {
             Object.DestroyImmediate(oldestEntry.image);
         }
     }
 }
 private static void ExpireCacheEntries()
 {
     while (CacheFull)
     {
         string key = null;
         CachedAssetStoreImage image = null;
         foreach (KeyValuePair <string, CachedAssetStoreImage> pair in CachedAssetStoreImages)
         {
             if ((image == null) || (image.lastUsed > pair.Value.lastUsed))
             {
                 image = pair.Value;
                 key   = pair.Key;
             }
         }
         CachedAssetStoreImages.Remove(key);
         AssetStorePreviewManager instance = Instance;
         instance.m_CacheRemove++;
         if (image == null)
         {
             Debug.LogError("Null entry found while removing cache entry");
             break;
         }
         if (image.client != null)
         {
             image.client.Abort();
             image.client = null;
         }
         if (image.image != null)
         {
             UnityEngine.Object.DestroyImmediate(image.image);
         }
     }
 }
        /**
         * Return a texture from a url that points to an image resource
         *
         * This method does not block but queues a request to fetch the image and return null.
         * When the image has been fetched this method will return the image texture downloaded.
         */
        public static CachedAssetStoreImage TextureFromUrl(string url, string label, int textureSize, GUIStyle labelStyle, GUIStyle iconStyle, bool onlyCached)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(Instance.m_DummyItem);
            }

            CachedAssetStoreImage cached;
            bool newentry = true;

            if (CachedAssetStoreImages.TryGetValue(url, out cached))
            {
                cached.lastUsed = EditorApplication.timeSinceStartup;

                // Refetch the image if the size has changed and is not in the progress of being fetched
                bool refetchInitiated = cached.requestedWidth == textureSize;
                bool correctSize      = cached.image != null && cached.image.width == textureSize;

                bool cacheRequestAborted = cached.requestedWidth == -1;

                if ((correctSize || refetchInitiated || onlyCached) && !cacheRequestAborted)
                {
                    Instance.CacheHit++;

                    // Use cached image (that may be null) if we're in progress of fetching the image
                    // or if we have rendered the images correctly
                    //return cached;
                    bool fetchingImage       = cached.client != null;
                    bool labelDrawn          = cached.label == null;
                    bool valid               = fetchingImage || labelDrawn;
                    bool convPerTickExceeded = Instance.m_ConvertedThisTick > kMaxConvertionsPerTick;
                    s_NeedsRepaint = s_NeedsRepaint || convPerTickExceeded;
                    return((valid || convPerTickExceeded) ?
                           cached :
                           RenderEntry(cached, labelStyle, iconStyle));
                }
                //Debug.Log(string.Format("Found {0} {1} {2} {3}", correctSize, refetchInitiated, onlyCached, cacheRequestAborted));
                newentry = false;
                if (Downloading >= kMaxConcurrentDownloads)
                {
                    return(cached.image == null ? Instance.m_DummyItem : cached);
                }
            }
            else
            {
                if (onlyCached || Downloading >= kMaxConcurrentDownloads)
                {
                    return(Instance.m_DummyItem);
                }
                cached          = new CachedAssetStoreImage();
                cached.image    = null;
                cached.lastUsed = EditorApplication.timeSinceStartup;
                //Debug.Log("url is " + textureSize.ToString() + " " + url);
            }

            // Only set fetch time when there is not image in order to use it for
            // fading in the image when it becomes available
            if (cached.image == null)
            {
                cached.lastFetched = EditorApplication.timeSinceStartup;
            }

            cached.requestedWidth = textureSize;
            cached.label          = label;

            AsyncHTTPClient client = null;

            client = SetupTextureDownload(cached, url, "previewSize-" + textureSize);

            ExpireCacheEntries();

            if (newentry)
            {
                CachedAssetStoreImages.Add(url, cached);
            }

            client.Begin();

            Instance.Requested++;
            return(cached);
        }