예제 #1
0
        /// <summary>Starts the loading, setting the current image to <see cref="_LoadingTexture"/>, if available. If the image is already in cache, and <paramref name="loadCachedIfAvailable"/>==true, will load that instead</summary>
        public void Load(string imageURL, bool loadCachedIfAvailable = true, LoadCompleteDelegate onCompleted = null, Action onCanceled = null)
        {
            bool currentRequestedURLAlreadyLoaded = _CurrentRequestedURL == imageURL;

            _CurrentRequestedURL = imageURL;

            if (loadCachedIfAvailable)
            {
                bool foundCached = false;
                // Don't re-request if the url is the same. This is useful if there's no pool provided
                if (currentRequestedURLAlreadyLoaded)
                {
                    foundCached = _Texture != null;
                }
                else if (_Pool != null)
                {
                    Texture2D cachedInPool = _Pool.Get(imageURL) as Texture2D;
                    if (cachedInPool)
                    {
                        _Texture             = cachedInPool;
                        foundCached          = true;
                        _CurrentRequestedURL = imageURL;
                    }
                }

                if (foundCached)
                {
                    _RawImage.texture = _Texture;
                    if (onCompleted != null)
                    {
                        onCompleted(true, true);
                    }

                    return;
                }
            }

            _RawImage.texture = _LoadingTexture;
            var request = new SimpleImageDownloader.Request()
            {
                url    = imageURL,
                onDone = result =>
                {
                    if (!_DestroyPending && imageURL == _CurrentRequestedURL) // this will be false if a new request was done during downloading, case in which the result will be ignored
                    {
                        // Commented: not reusing textures to load data into them anymore, since in most cases we'll use a pool
                        //result.LoadTextureInto(_Texture);

                        if (_Pool == null)
                        {
                            // Non-pooled textures should be destroyed
                            if (_Texture)
                            {
                                DisposeTexture(_Texture);
                            }

                            _Texture = result.CreateTextureFromReceivedData();
                        }
                        else
                        {
                            var  textureAlreadyStoredMeanwhile = _Pool.Get(imageURL);
                            bool someoneStoredTheImageSooner   = textureAlreadyStoredMeanwhile != null;
                            if (someoneStoredTheImageSooner)
                            {
                                // Happens when the same URL is requested multiple times for the first time, and of course only the first
                                // downloaded image should be kept. In this case, someone else already have downloaded and cached the image, so we just discard the one we downloaded
                                _Texture = textureAlreadyStoredMeanwhile as Texture2D;
                            }
                            else
                            {
                                // First time downloaded => cache
                                _Texture = result.CreateTextureFromReceivedData();
                                _Pool.Put(imageURL, _Texture);
                            }
                        }

                        _RawImage.texture = _Texture;

                        if (onCompleted != null)
                        {
                            onCompleted(false, true);
                        }
                    }
                    else if (onCanceled != null)
                    {
                        onCanceled();
                    }
                },
                onError = () =>
                {
                    if (!_DestroyPending && imageURL == _CurrentRequestedURL)                     // this will be false if a new request was done during downloading, case in which the result will be ignored
                    {
                        _RawImage.texture = _ErrorTexture;

                        if (onCompleted != null)
                        {
                            onCompleted(false, false);
                        }
                    }
                    else if (onCanceled != null)
                    {
                        onCanceled();
                    }
                }
            };

            SimpleImageDownloader.Instance.Enqueue(request);
        }
        /// <summary>Starts the loading, setting the current image to <see cref="_LoadingTexture"/>, if available. If the image is already in cache, and <paramref name="loadCachedIfAvailable"/>==true, will load that instead</summary>
        public void Load(string imageURL, bool loadCachedIfAvailable = true, Action <bool, bool> onCompleted = null, Action onCanceled = null)
        {
            // Don't download the same image again
            if (loadCachedIfAvailable && _CurrentRequestedURL == imageURL)
            {
                if (_RecycledTexture)
                {
                    if (_RecycledTexture != _RawImage.texture)
                    {
                        _RawImage.texture = _RecycledTexture;
                    }

                    if (onCompleted != null)
                    {
                        onCompleted(true, true);
                    }

                    return;
                }
            }

            _CurrentRequestedURL = imageURL;
            _RawImage.texture    = _LoadingTexture;
            var request = new SimpleImageDownloader.Request()
            {
                url    = imageURL,
                onDone = result =>
                {
                    if (!_DestroyPending && imageURL == _CurrentRequestedURL) // this will be false if a new request was done during downloading, case in which the result will be ignored
                    {
                        if (_RecycledTexture)
                        {
                            result.LoadTextureInto(_RecycledTexture);
                        }
                        else
                        {
                            _RecycledTexture = result.CreateTextureFromReceivedData();
                        }
                        _RawImage.texture = _RecycledTexture;

                        if (onCompleted != null)
                        {
                            onCompleted(false, true);
                        }
                    }
                    else if (onCanceled != null)
                    {
                        onCanceled();
                    }
                },
                onError = () =>
                {
                    if (!_DestroyPending && imageURL == _CurrentRequestedURL)                     // this will be false if a new request was done during downloading, case in which the result will be ignored
                    {
                        _RawImage.texture = _ErrorTexture;

                        if (onCompleted != null)
                        {
                            onCompleted(false, false);
                        }
                    }
                    else if (onCanceled != null)
                    {
                        onCanceled();
                    }
                }
            };

            SimpleImageDownloader.Instance.Enqueue(request);
        }