123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using BestHTTP;
- namespace BestHTTP.Examples
- {
- public sealed class LargeFileDownloadSample : MonoBehaviour
- {
-
-
-
- const string URL = "http://uk3.testmy.net/dl-102400";
- #region Private Fields
-
-
-
- HTTPRequest request;
-
-
-
- string status = string.Empty;
-
-
-
- float progress;
-
-
-
- int fragmentSize = HTTPResponse.MinBufferSize;
- #endregion
- #region Unity Events
- void Awake()
- {
-
- if (PlayerPrefs.HasKey("DownloadLength"))
- progress = PlayerPrefs.GetInt("DownloadProgress") / (float)PlayerPrefs.GetInt("DownloadLength");
- }
- void OnDestroy()
- {
-
- if (request != null && request.State < HTTPRequestStates.Finished)
- {
- request.OnProgress = null;
- request.Callback = null;
- request.Abort();
- }
- }
- void OnGUI()
- {
- GUIHelper.DrawArea(GUIHelper.ClientArea, true, () =>
- {
-
- GUILayout.Label("Request status: " + status);
- GUILayout.Space(5);
-
- GUILayout.Label(string.Format("Progress: {0:P2} of {1:N0}Mb", progress, PlayerPrefs.GetInt("DownloadLength") / 1048576 ));
- GUILayout.HorizontalSlider(progress, 0, 1);
- GUILayout.Space(50);
- if (request == null)
- {
-
- GUILayout.Label(string.Format("Desired Fragment Size: {0:N} KBytes", fragmentSize / 1024f));
- fragmentSize = (int)GUILayout.HorizontalSlider(fragmentSize, HTTPResponse.MinBufferSize, 10 * 1024 * 1024);
- GUILayout.Space(5);
- string buttonStr = PlayerPrefs.HasKey("DownloadProgress") ? "Continue Download" : "Start Download";
- if (GUILayout.Button(buttonStr))
- StreamLargeFileTest();
- }
- else if (request.State == HTTPRequestStates.Processing && GUILayout.Button("Abort Download"))
- {
-
- request.Abort();
- }
- });
- }
- #endregion
- #region Private Helper Functions
-
-
- void StreamLargeFileTest()
- {
- request = new HTTPRequest(new Uri(URL), (req, resp) =>
- {
- switch (req.State)
- {
-
- case HTTPRequestStates.Processing:
-
- if (!PlayerPrefs.HasKey("DownloadLength"))
- {
- string value = resp.GetFirstHeaderValue("content-length");
- if (!string.IsNullOrEmpty(value))
- PlayerPrefs.SetInt("DownloadLength", int.Parse(value));
- }
-
- ProcessFragments(resp.GetStreamedFragments());
- status = "Processing";
- break;
-
- case HTTPRequestStates.Finished:
- if (resp.IsSuccess)
- {
-
- ProcessFragments(resp.GetStreamedFragments());
-
- if (resp.IsStreamingFinished)
- {
- status = "Streaming finished!";
-
- PlayerPrefs.DeleteKey("DownloadProgress");
- PlayerPrefs.Save();
- request = null;
- }
- else
- status = "Processing";
- }
- else
- {
- status = string.Format("Request finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
- resp.StatusCode,
- resp.Message,
- resp.DataAsText);
- Debug.LogWarning(status);
- request = null;
- }
- break;
-
- case HTTPRequestStates.Error:
- status = "Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception");
- Debug.LogError(status);
- request = null;
- break;
-
- case HTTPRequestStates.Aborted:
- status = "Request Aborted!";
- Debug.LogWarning(status);
- request = null;
- break;
-
- case HTTPRequestStates.ConnectionTimedOut:
- status = "Connection Timed Out!";
- Debug.LogError(status);
- request = null;
- break;
-
- case HTTPRequestStates.TimedOut:
- status = "Processing the request Timed Out!";
- Debug.LogError(status);
- request = null;
- break;
- }
- });
-
- if (PlayerPrefs.HasKey("DownloadProgress"))
-
- request.SetRangeHeader(PlayerPrefs.GetInt("DownloadProgress"));
- else
-
- PlayerPrefs.SetInt("DownloadProgress", 0);
- #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR)
-
- request.DisableCache = true;
- #endif
-
- request.UseStreaming = true;
-
- request.StreamFragmentSize = fragmentSize;
-
- request.Send();
- }
-
-
-
- void ProcessFragments(List<byte[]> fragments)
- {
- if (fragments != null && fragments.Count > 0)
- {
-
- for (int i = 0; i < fragments.Count; ++i)
- {
-
- int downloaded = PlayerPrefs.GetInt("DownloadProgress") + fragments[i].Length;
- PlayerPrefs.SetInt("DownloadProgress", downloaded);
- }
- PlayerPrefs.Save();
-
- progress = PlayerPrefs.GetInt("DownloadProgress") / (float)PlayerPrefs.GetInt("DownloadLength");
- }
- }
- #endregion
- }
- }
|