Exemplo n.º 1
0
 public static DownloadManagerHelper GetDownloadManagerHelper()
 {
     if (instance == null)
     {
         instance = (new GameObject("DownloadManagerHelper")).AddComponent <DownloadManagerHelper>();
     }
     return(instance);
 }
Exemplo n.º 2
0
        /// <summary>
        /// 开始下载文件
        /// </summary>
        /// <param name="isRange">是否采用断点续传的方式进行下载</param>
        public void StartDownload(bool isRange = true)
        {
            if (IsDone)
            {
                return;
            }
            if (URL == null || "".Equals(URL))
            {
                SystemErrorMsg = "URL不能为空";
                CompletedFinally();
                return;
            }
            if (SavePath == null || "".Equals(SavePath))
            {
                SystemErrorMsg = "保存路径不能为空";
                CompletedFinally();
                return;
            }
            UnityWebRequest = new UnityWebRequest(URL, Method);
            IsRange         = isRange;
            //如果下载目录不存在,则创建目录及其父目录
            if (!FileTools.DirectoryExists(SavePath))
            {
                try
                {
                    FileTools.CreateDirectory(SavePath);
                }
                catch (System.UnauthorizedAccessException)
                {
                    SystemErrorMsg = "没有权限";
                    CompletedFinally();
                    return;
                }
                catch (System.IO.IOException)
                {
                    SystemErrorMsg = "目录路径有误";
                    CompletedFinally();
                    return;
                }
                catch (System.Exception e)
                {
                    SystemErrorMsg = e.Message;
                    CompletedFinally();
                    return;
                }
            }
            //如果没有设置文件名称,则通过URL来确定临时文件名称
            string tempName = null;

            try
            {
                tempName = SaveName == null?FileTools.GetFileName(URL).Split('?')[0] : SaveName;
            }
            catch (System.ArgumentException)
            {
                //如果无法通过URL获取到下载名称,则通过HashCode来确定名称
                tempName = ((uint)URL.GetHashCode()).ToString();
            }
            TempFilePath  = FileTools.PathCombine(SavePath, tempName);
            TempFilePath += ".temp";
            try
            {
                if (!IsRange && FileTools.FileExists(TempFilePath))
                {
                    FileTools.DeleteFile(TempFilePath);
                }
            }
            catch (System.Exception e)
            {
                SystemErrorMsg = e.Message;
                CompletedFinally();
                return;
            }
            try
            {
                DownloadHandlerRange = new DownloadHandlerRange(TempFilePath, UnityWebRequest);
                DownloadHandlerRange.StartDownloadEvent += StartDownloadCallBack;
            }
            catch (System.IO.IOException)
            {
                SystemErrorMsg = "文件正在被下载";
                CompletedFinally();
                return;
            }
            catch (System.Exception e)
            {
                SystemErrorMsg = e.Message;
                CompletedFinally();
                return;
            }
            UnityWebRequest.downloadHandler = DownloadHandlerRange;
#if UNITY_2017_2_OR_NEWER
            UnityWebRequest.SendWebRequest().completed += Download_completed;
#else
            UnityWebRequest.Send();
            DownloadManagerHelper.GetDownloadManagerHelper().StartCoroutine(JudgmentCompleted());
#endif
        }