예제 #1
0
        private static IEnumerator _DownloadFile(string url, string savePath,
                                                 OnRequestSuccess onDownloadStart,
                                                 OnRequestSuccess onFinish,
                                                 OnRequestError onError,
                                                 OnRequestProgress onProgress)
        {
            var headRequest = UnityWebRequest.Head(url);

            yield return(headRequest.SendWebRequest());

            var totalSize = long.Parse(headRequest.GetResponseHeader("Content-Length"));
            var fileInfo  = new FileInfo(savePath);

            if (fileInfo.Exists && fileInfo.Length == totalSize)
            {
                onFinish?.Invoke(headRequest);
            }
            else
            {
                using (var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET))
                {
                    var handler = new DownloadHandlerFileRange(savePath, request);
                    request.downloadHandler = handler;
                    onDownloadStart?.Invoke(request);

                    var asyncOp = request.SendWebRequest();
                    while (!asyncOp.isDone)
                    {
                        yield return(null);

                        onProgress?.Invoke(asyncOp);
                    }

                    if (request.isNetworkError || request.isHttpError)
                    {
                        handler.Close();
                        if (onError != null)
                        {
                            Debug.LogWarningFormat("SendRequest Failed:\nresponseCode :{0}\nerror :{1}\nurl:{2}",
                                                   request.responseCode, request.error, request.url);
                            onError(request);
                        }
                        else
                        {
                            Debug.LogErrorFormat("SendRequest Failed:\nresponseCode :{0}\nerror :{1}\nurl:{2}",
                                                 request.responseCode, request.error, request.url);
                        }
                    }
                    else
                    {
                        Debug.LogFormat("Finish UnityWebRequest: {0}\nresponseCode :{1}", request.url,
                                        request.responseCode);
                        onFinish?.Invoke(request);
                    }
                }
            }
        }
 public void RequestSuccess()
 {
     OnRequestSuccess?.Invoke();
 }