示例#1
0
        // 记录在文本中
        public static IEnumerator GetText(string url, string txtPath)
        {
            string dir = Path.GetDirectoryName(txtPath);

            if (string.IsNullOrEmpty(dir))
            {
                MyLog.Red("这个路径得出的文件夹路径为空 —— " + txtPath);
                yield break;
            }
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                yield return(request.SendWebRequest());

                MyIO.FileExistsDelte(txtPath);
                using (StreamWriter sw = File.CreateText(txtPath))
                {
                    sw.Write(request.downloadHandler.text);
                    sw.Close();
                }
            }
        }
示例#2
0
        public static IEnumerator GetFile(string url, string fileFullPath, int timeOut, Action onFinish, Action <float> onProgress)
        {
            MyIO.FileExistsDelte(fileFullPath);
            string dir = Path.GetDirectoryName(fileFullPath);

            if (string.IsNullOrEmpty(dir))
            {
                MyLog.Red("这个路径得出的文件夹路径为空 —— " + fileFullPath);
                yield break;
            }
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (UnityWebRequest request = UnityWebRequest.Get(url))
            {
                if (timeOut > 0)
                {
                    request.timeout = timeOut;
                }
                DownloadHandlerFile file = new DownloadHandlerFile(fileFullPath);
                file.removeFileOnAbort = true;                   // 下载被中止或错误删除创建的文件
                request.disposeUploadHandlerOnDispose = true;
                request.downloadHandler = file;
                UnityWebRequestAsyncOperation asy = request.SendWebRequest();
                while (!asy.isDone)
                {
                    if (null != onProgress)
                    {
                        onProgress(asy.progress);
                    }
                    yield return(null);
                }
                if (isNoError(request) && null != onFinish)
                {
                    onFinish();
                }
            }
            yield return(0);
        }