Пример #1
0
        protected virtual void RequestImage_Internal <E>(IMultiSizeImageLocator <E> locator, E size,
                                                         Func <Texture2D> loadFromDisk,
                                                         Action <Texture2D> saveToDisk,
                                                         Action <Texture2D> onSuccess,
                                                         Action <Texture2D> onFallback,
                                                         Action <WebRequestError> onError)
        {
            Debug.Assert(locator != null);

            string url = locator.GetSizeURL(size);

            // check cache and existing callbacks
            if (this.TryGetCacheOrSetCallbacks(url, onSuccess, onFallback, onError))
            {
                return;
            }

            // - Start new request -
            Callbacks callbacks = this.CreateCallbacksEntry(url, onSuccess, onError);

            // check for fallback
            callbacks.fallback = this.FindFallbackTexture(locator);

            if (onFallback != null &&
                callbacks.fallback != null)
            {
                onFallback.Invoke(callbacks.fallback);
            }

            // add save function to download callback
            callbacks.onTextureDownloaded = saveToDisk;

            // start process by checking the cache
            Texture2D texture = loadFromDisk();

            if (texture != null)
            {
                this.OnRequestSucceeded(url, texture);
            }
            else
            {
                // do the download
                this.DownloadImage(url);
            }
        }
Пример #2
0
        /// <summary>Handles computations for the image request.</summary>
        protected virtual void RequestImage_Internal <E>(IMultiSizeImageLocator <E> locator, E size,
                                                         Func <Texture2D> loadFromDisk,
                                                         Action <Texture2D> saveToDisk,
                                                         Action <Texture2D> onSuccess,
                                                         Action <Texture2D> onFallback,
                                                         Action <WebRequestError> onError)
        {
            Debug.Assert(locator != null);
            Debug.Assert(onSuccess != null);

            // init vars
            string    url       = locator.GetSizeURL(size);
            Callbacks callbacks = null;

            // check for null URL
            if (string.IsNullOrEmpty(url))
            {
                #if UNITY_EDITOR
                if (!this.excludeDownloadsFromLogs)
                {
                    Debug.Log("[mod.io] Attempted to fetch image with a Null or Empty"
                              + " url in the locator.");
                }
                #endif

                onSuccess(null);
                return;
            }

            // check cache
            Texture2D texture = null;
            if (this.cache.TryGetValue(url, out texture))
            {
                onSuccess(texture);
                return;
            }

            // check currently downloading
            if (this.m_callbackMap.TryGetValue(url, out callbacks))
            {
                // add callbacks
                callbacks.succeeded.Add(onSuccess);
                if (onError != null)
                {
                    callbacks.failed.Add(onError);
                }

                // check for fallback
                if (onFallback != null)
                {
                    Texture2D fallback = FindFallbackTexture(locator);
                    if (fallback != null)
                    {
                        onFallback(fallback);
                    }
                }

                return;
            }

            // check disk
            if (loadFromDisk != null)
            {
                texture = loadFromDisk();
                if (texture != null)
                {
                    this.cache.Add(url, texture);
                    onSuccess(texture);
                    return;
                }
            }

            // create new callbacks entry
            callbacks = new Callbacks()
            {
                succeeded = new List <Action <Texture2D> >(),
                failed    = new List <Action <WebRequestError> >(),
            };
            this.m_callbackMap.Add(url, callbacks);

            // add functions
            if (saveToDisk != null)
            {
                callbacks.succeeded.Add(saveToDisk);
            }
            callbacks.succeeded.Add(onSuccess);

            // check for fallback
            if (onFallback != null)
            {
                Texture2D fallback = FindFallbackTexture(locator);
                if (fallback != null)
                {
                    onFallback(fallback);
                }
            }

            // start download
            this.DownloadImage(url);
        }