Пример #1
0
        public void StartLoad()
        {
            state = LoadState.run;
            string url       = loadList[0];
            string extension = Path.GetExtension(url).ToUpper();
            string imgName   = FileExeUtil.MD5Encrypt(url) + extension;
            string path      = OnlineTextureManager.ImageCachePath + imgName;

            StartCoroutine(DownLoad(url, path));
        }
Пример #2
0
        public bool  HadCachedInLocal(string url)
        {
            url = url.Replace(" ", "%20");
            string extension = Path.GetExtension(url).ToUpper();
            string imgName   = FileExeUtil.MD5Encrypt(url) + extension;           //根据URL获取文件的名字

            if (File.Exists(ImageCachePath + imgName))
            {
                return(true);
            }
            return(false);
        }
Пример #3
0
        /// <summary>在没有Tween运行的时候队列加载图片
        /// </summary>
        //void PopRequestStack()
        //{
        //    if (LoadTextureRequests.Count > 0&&iTween.tweens.Count==0)
        //    {
        //        Hashtable ht = LoadTextureRequests.Dequeue();
        //        Debug.Log(ht["url"]);
        //        Debug.Log(ht["callback"]);
        //        LoadTexture((string)ht["url"], (Action<Texture2D>)ht["callback"], (int)ht["width"], (int)ht["hight"]);
        //    }
        //}

        void LoadTexture(string url, Action <Texture2D, string> callback, int width, int hight, bool isCacheDisk = false, float compressFactor = 1.0f)
        {
            url = url.Replace(" ", "%20");
            string extension = Path.GetExtension(url).ToUpper();
            string imgName   = FileExeUtil.MD5Encrypt(url) + extension;           //根据URL获取文件的名字

            if (File.Exists(ImageCachePath + imgName))
            {
                LoadLocalFile(url, callback, width, hight);
            }
            else
            {
                LoadNetFile(url, callback, isCacheDisk, compressFactor);
            }
        }
Пример #4
0
        private void LoadLocalFile(string url, Action <Texture2D, string> callback, int width, int hight)
        {
            var imageCachePath = ImageCachePath;

#if UNITY_EDITOR
            string     extension  = Path.GetExtension(url).ToUpper();
            string     imgName    = FileExeUtil.MD5Encrypt(url) + extension; //根据URL获取文件的名字
            FileStream fs         = File.OpenRead(imageCachePath + imgName); //OpenRead
            int        filelength = (int)fs.Length;                          //获得文件长度
            var        image      = new Byte[filelength];                    //建立一个字节数组
            fs.Read(image, 0, filelength);                                   //按字节流读取

            var text = new Texture2D(width, hight);
            text.LoadImage(image);
            callback(text, url);
            if (!textureDic.ContainsKey(url))
            {
                textureDic.Add(url, text);
            }
#else
            Loom.RunAsync(() =>
            {
                string extension = Path.GetExtension(url).ToUpper();
                string imgName   = FileExeUtil.MD5Encrypt(url) + extension; //根据URL获取文件的名字
                FileStream fs    = File.OpenRead(imageCachePath + imgName); //OpenRead
                int filelength   = (int)fs.Length;                          //获得文件长度
                var image        = new Byte[filelength];                    //建立一个字节数组
                fs.Read(image, 0, filelength);                              //按字节流读取
                Loom.QueueOnMainThread(() =>
                {
                    var text = new Texture2D(width, hight);
                    text.LoadImage(image);
                    callback(text, url);
                    if (!textureDic.ContainsKey(url))
                    {
                        textureDic.Add(url, text);
                    }
                });
            });
#endif
        }
Пример #5
0
        /// <summary>
        /// 开始加载资源
        /// </summary>
        /// <param name="url">URL.</param>
        /// <param name="callback">Callback.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        public void StartLoad(string url, Action <Texture2D, string> callback, bool isCacheDisk = false, int loadwidth = 0, int loadhight = 0, float compressFactor = 1.0f)
        {
            //==Start========移到LoadTexture方法中============
            url = url.Replace(" ", "%20");
            string extension = Path.GetExtension(url).ToUpper();
            string imgName   = FileExeUtil.MD5Encrypt(url) + extension;           //根据URL获取文件的名字

            if (textureDic.ContainsKey(url) && textureDic[url] != null)
            {
                callback?.Invoke(textureDic[url], url);
                return;
            }
            if (File.Exists(ImageCachePath + imgName))
            {
                LoadLocalFile(url, callback, loadwidth, loadhight);
            }
            else
            {
                LoadNetFile(url, callback, isCacheDisk, compressFactor);
            }
            //==End========移到LoadTexture方法中============
        }
Пример #6
0
        protected IEnumerator DownLoad(string url, string path)
        {
            loadRequst = UnityWebRequest.Get(url);
            DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);

            loadRequst.downloadHandler = downloadTexture;
            yield return(loadRequst.SendWebRequest());

            //loadWWW = new WWW(url);
            // yield return loadWWW;
            if (loadRequst.error == null)
            {
                try
                {
                    //  Texture2D texture = FileExeUtil.ScaleTexture(loadWWW.texture, (int)(loadWWW.texture.width / 2.5), (int)(loadWWW.texture.height / 2.5));
                    Texture2D texture = downloadTexture.texture;
                    if (_compressFactorDictionary.ContainsKey(url) && 1.0f - _compressFactorDictionary[url] > 0.1f)
                    {
                        texture = FileExeUtil.ScaleTexture(downloadTexture.texture, (int)(downloadTexture.texture.width * _compressFactorDictionary[url]), (int)(downloadTexture.texture.height * _compressFactorDictionary[url]));
                    }

                    if (CompleteCallback != null)
                    {
                        CompleteCallback(url, texture);
                    }
                    if (!Directory.Exists(FileCacheManager.ImageCachePath))
                    {
                        Directory.CreateDirectory(FileCacheManager.ImageCachePath);
                    }
                    Debug.Log("ImageCachePath" + FileCacheManager.ImageCachePath);
                    if (_isCacheDiskDictionary[url])
                    {
                        byte[] bytes = FileExeUtil.EncodeTexture(texture, path);
                        if (bytes == null)
                        {
                            bytes = loadRequst.downloadHandler.data;
                        }
                        SaveAsync(bytes, path);
                    }
                }
                catch (Exception e)
                {
                    Debug.Log("!!!!!!!!!!!DownLoadToLocal:" + e.ToString());
                    if (CompleteCallback != null)
                    {
                        CompleteCallback(url, downloadTexture.texture);
                    }
                }
            }
            else
            {
                Debug.Log("DownLoad Image url : " + url + "  error : " + loadRequst.error);
                if (CompleteCallback != null)
                {
                    CompleteCallback(url, null);
                }
            }
            //		loadWWW.Dispose ();
            Resources.UnloadUnusedAssets();
            loadRequst = null;
            loadList.Remove(url);
            if (loadList.Count <= 0)
            {
                state = LoadState.stop;
            }
        }