/// <summary> /// 下载图片接口 /// </summary> /// <param name="url"></param> /// <param name="downLoadAct">下载成功或者失败后的回调</param> public void DownLoadTexture(string url, Action <string, string> downLoadAct) { #region Url 检测 if (string.IsNullOrEmpty(url)) { if (downLoadAct != null) { downLoadAct("", url); } Debug.LogError("Url Should Not Be Null or Empty "); return; } if (url.StartsWith("http") == false) { url = "http://" + url; //手机端必须以 http:// 开始否则无法识别地址 } #endregion if (m_AllLocalCacheTexturesDic.ContainsKey(url)) { //#if UNITY_EDITOR // DataShow_TextureDownLoad.Insance.FlushView(); //#endif Debug.Log("The File Is Exit ,Load Cache Image >>> " + url); if (downLoadAct != null) { downLoadAct(m_AllLocalCacheTexturesDic[url], url); //本地已经缓存了 } return; } if (CurrentTaskCount >= MaxTaskCount) { //#if UNITY_EDITOR // DataShow_TextureDownLoad.Insance.FlushView(); //#endif Debug.Log("Delay DownLoad .... DelayList Count=" + m_WaitingList.Count); m_WaitingList.Enqueue(new TextureDownLoadHeplerCacheData(url, downLoadAct)); return; } //当下载任务过多时候 进行限制 //DownLoad Texture string fileFullPath = FileDirHelper.GetUrlFileDir(url); fileFullPath = GetLocalCachePath(fileFullPath); Debug.Log("Start DownLoad " + url + " fileFullPath=" + fileFullPath); ++CurrentTaskCount; EventCenter.GetInstance().StartCoroutine(EventCenter.GetInstance().DownLoadAction(url, fileFullPath, downLoadAct, DownLoad)); }
/// <summary> /// Http DowmLoad Fuc /// </summary> /// <param name="httpUrl">Url </param> /// <param name="callback">DoewnLoad Complete CallBack string Is FilePath(maybe null Or "") ,Byte[] Is RealData(maybe null) ,Noty Get byte[] data first</param> /// <param name="sender">sender </param> /// <param name="_isLocalSave">true for default ,fase will save sa cache</param> /// <param name="_IsFilePath">false default to identify the callback path is fileName , true is filePath</param> /// public static void HttpDownLoad(string httpUrl, Action <string> callback, GameObject sender) { //Debug.Log("当前任务数量 " + currentTaskCount); if (sender == null || sender.activeSelf == false) { return; } if (string.IsNullOrEmpty(httpUrl)) { return; } if (httpUrl.StartsWith("http") == false) { httpUrl = "http://" + httpUrl; } if (currentTaskCount >= MaxTaskCount) {//Delay Dowload request EventCenter.GetInstance().DelayDoEnumerator(0.1f, () => { HttpDownLoad(httpUrl, callback, sender); }); return; } #region File Path string _fileRelativeNamePath = FileDirHelper.GetFilePathWithOneDir(httpUrl); //getfile path with one dicrionary path and fileName . if (Directory.Exists(Application.persistentDataPath + "/beanvr") == false) { Directory.CreateDirectory(Application.persistentDataPath + "/beanvr"); //create dictionary beanvr } #endregion string localPath = Application.persistentDataPath + "/Beanvr/" + _fileRelativeNamePath; //保存到本地的目录以及名称 //Debug.Log("xxxxxxxxxxxx " + localPath); if (File.Exists(localPath)) { HttpDown_FileExit(httpUrl, callback, sender, localPath); } else { HttpDown_FileNotExit(httpUrl, callback, sender, localPath); } }
static void HttpDown_FileExit(string httpUrl, Action <string> callback, GameObject sender, string localPath) { string _fileRelativeNamePath = FileDirHelper.GetFilePathWithOneDir(httpUrl); //getfile path with one dicrionary path and fileName . #region 文件存在 //Debug.Log("File Exit ::" + localPath); HttpProtocolHelper httpHead = new HttpProtocolHelper(); httpHead.m_Url = httpUrl; httpHead.m_Method = "HEAD"; httpHead.m_HttpFinishDownHandle = (bool isHttpDownLoadOk, string _FileName, HttpWebResponse response) => { Loom.QueueOnMainThread(() => { //Go to Main Thread CheckWhetherNeedToDown(localPath, response, _fileRelativeNamePath, httpUrl, callback); }); //main thread }; //handle currentTaskCount++; ////下载头信息 httpHead.GetHttpResponse(); #endregion }