示例#1
0
    public static ImageCache DownloadImage(Image img, ImageCache.DownloadCallback callback)
    {
        if (img == null || img.mUrl == null)
        {
            return(null);
        }
        ImageCache ic;

        if (ImageCache.EntriesByUrl.TryGetValue(img.mUrl, out ic))
        {
            if (ic.Texture != null || ic.Progress >= 0f || ic.Failed)
            {
                img.mImgCache = ic;
                return(ic);
            }
        }
        else
        {
            ImageCache.EnsureCacheSpace();
            ic                = new ImageCache();
            ic.Failed         = false;
            ic.Texture        = null;
            ic.m_DownloadedAt = -1.0;
            ic.LastUsed       = EditorApplication.timeSinceStartup;
            ImageCache.EntriesByUrl[img.mUrl] = ic;
        }
        img.mImgCache = ic;
        ic.Progress   = 0f;
        AssetStoreClient.ProgressCallback progress = delegate(double pctUp, double pctDown)
        {
            ic.Progress = (float)pctDown;
        };
        AssetStoreClient.DoneCallback callback2 = delegate(AssetStoreResponse resp)
        {
            ic.Progress       = -1f;
            ic.LastUsed       = EditorApplication.timeSinceStartup;
            ic.m_DownloadedAt = ic.LastUsed;
            ic.Failed         = resp.failed;
            if (resp.ok && resp.binData != null && resp.binData.Length > 0)
            {
                Texture2D texture2D = new Texture2D(2, 2, TextureFormat.ARGB32, false);
                texture2D.LoadImage(resp.binData);
                ic.Texture = texture2D;
            }
            if (callback != null)
            {
                string text = string.Format("Error fetching {0}", img.Name);
                if (resp.failed)
                {
                    DebugUtils.LogWarning(text + string.Format(" : ({0}) {1} from {2}", resp.HttpStatusCode, resp.HttpErrorMessage ?? "n/a", img.mUrl));
                }
                callback(img, ic, (!resp.ok) ? text : null);
            }
        };
        AssetStoreClient.LoadFromUrl(img.mUrl, callback2, progress);
        return(ic);
    }
    public static ImageCache DownloadImage(Image img, ImageCache.DownloadCallback callback)
    {
        ImageCache imageCache;

        if (img == null || img.mUrl == null)
        {
            return(null);
        }

        if (!ImageCache.EntriesByUrl.TryGetValue(img.mUrl, out imageCache))
        {
            ImageCache.EnsureCacheSpace();
            imageCache = new ImageCache()
            {
                Failed         = false,
                Texture        = null,
                m_DownloadedAt = -1,
                LastUsed       = EditorApplication.timeSinceStartup
            };
            ImageCache.EntriesByUrl[img.mUrl] = imageCache;
        }
        else if (imageCache.Texture != null || imageCache.Progress >= 0f || imageCache.Failed)
        {
            img.mImgCache = imageCache;
            return(imageCache);
        }

        img.mImgCache       = imageCache;
        imageCache.Progress = 0f;
        AssetStoreClient.ProgressCallback progress     = (double pctUp, double pctDown) => imageCache.Progress = (float)pctDown;
        AssetStoreClient.DoneCallback     doneCallback = (AssetStoreResponse resp) =>
        {
            imageCache.Progress       = -1f;
            imageCache.LastUsed       = EditorApplication.timeSinceStartup;
            imageCache.m_DownloadedAt = imageCache.LastUsed;
            imageCache.Failed         = resp.failed;
            if (resp.ok && resp.binData != null && (int)resp.binData.Length > 0)
            {
                Texture2D texture2D = new Texture2D(2, 2, TextureFormat.ARGB32, false);
                texture2D.LoadImage(resp.binData);
                imageCache.Texture = texture2D;
            }

            if (callback != null)
            {
                string str = string.Format("Error fetching {0}", img.Name);
                if (resp.failed)
                {
                    DebugUtils.LogWarning(string.Concat(str, string.Format(" : ({0}) {1} from {2}", resp.HttpStatusCode, resp.HttpErrorMessage ?? "n/a", img.mUrl)));
                }

                callback(img, imageCache, (!resp.ok ? str : null));
            }
        };
        AssetStoreClient.LoadFromUrl(img.mUrl, doneCallback, progress);
        return(imageCache);
    }