private void OnOperationCompleted(AsyncOperation opt) { var operation = opt as UnityWebRequestAsyncOperation; var request = operation.webRequest; if (request.isNetworkError || request.isHttpError || info.currentSize < info.totalSize) { state = DownloadState.error; } else { state = DownloadState.done; } m_FileDownloadHandler.CloseStream(); m_FileDownloadHandler = null; request.Dispose(); }
public void Start() { state = DownloadState.downloading; var dirname = Path.GetDirectoryName(info.path); if (!Directory.Exists(dirname)) { Directory.CreateDirectory(dirname); } if (info.currentSize == info.totalSize) { state = DownloadState.done; } else { m_FileDownloadHandler = new FileDownloadHandler(info); if (!m_FileDownloadHandler.OpenOrCreateFile()) { state = DownloadState.error; m_FileDownloadHandler.CloseStream(); } else { var request = UnityWebRequest.Get(info.url); request.timeout = 60; request.redirectLimit = 1; request.downloadHandler = m_FileDownloadHandler; if (info.currentSize == 0) { request.SetRequestHeader("Range", "bytes=0-"); } else { request.SetRequestHeader("Range", "bytes=" + info.currentSize + "-" + info.totalSize); } var operation = request.SendWebRequest(); operation.completed += OnOperationCompleted; } } }