Exemplo n.º 1
0
    IEnumerator StartDownloadFile()
    {
        while (state == DownloadState.Downloading && downloadList.Count > 0)
        {
            DownloadFile file = downloadList[0];
            file.state = DownloadState.Downloading;
            Debug.Log("FileDownloader >> Start Download:" + file.url);
            if (onFileDownloadStart != null)
            {
                onFileDownloadStart(this, file.url);
            }

            m_uwr = new UnityWebRequest(file.url);
            m_uwr.downloadHandler = new DownloadHandlerFile(file.savePath);
            m_uwr.timeout         = m_timeout;
            yield return(m_uwr.SendWebRequest());

            if (m_uwr.isNetworkError || m_uwr.isHttpError)
            {
                if (m_currentRetryTimes < m_retrytimes)
                {
                    ++m_currentRetryTimes;
                    Debug.Log("retry_time:" + m_currentRetryTimes);
                    continue;
                }
                m_currentRetryTimes = 0;
                if (m_uwr.isNetworkError)
                {
                    file.ChangeStateTo(DownloadState.NetworkError);
                    ChangeStateTo(DownloadState.NetworkError);
                    break;
                }

                if (m_uwr.isHttpError)
                {
                    file.ChangeStateTo(DownloadState.HttpError);
                    ChangeStateTo(DownloadState.HttpError);
                    break;
                }
            }

            if (m_uwr.isDone)
            {
                m_currentRetryTimes   = 0;
                file.state            = DownloadState.Finish;
                m_downloadedFileSize += (m_uwr.downloadedBytes / 1024);
                if (onFileDownloadFinish != null)
                {
                    onFileDownloadFinish(this, file.url);
                }

                downloadList.RemoveAt(0);
                yield return(null);
            }
        }

        if (downloadList.Count == 0)
        {
            m_uwr.Dispose();
            ChangeStateTo(DownloadState.Finish);
        }

        yield return(null);
    }