ResumableStreamingSample.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using BestHTTP;
  6. namespace BestHTTP.Examples.HTTP
  7. {
  8. public sealed class ResumableStreamingSample : StreamingSample
  9. {
  10. const string ProcessedBytesKey = "ProcessedBytes";
  11. const string DownloadLengthKey = "DownloadLength";
  12. /// <summary>
  13. /// Expected content length
  14. /// </summary>
  15. protected override long DownloadLength { get { return PlayerPrefs.GetInt(this._downloadPath + DownloadLengthKey); } set { PlayerPrefs.SetInt(this._downloadPath + DownloadLengthKey, (int)value); } }
  16. /// <summary>
  17. /// Total processed bytes
  18. /// </summary>
  19. protected override long ProcessedBytes { get { return PlayerPrefs.GetInt(this._downloadPath + ProcessedBytesKey, 0); } set { PlayerPrefs.SetInt(this._downloadPath + ProcessedBytesKey, (int)value); } }
  20. private long downloadStartedAt = 0;
  21. protected override void Start()
  22. {
  23. base.Start();
  24. // If we have a non-finished download, set the progress to the value where we left it
  25. float progress = GetSavedProgress();
  26. if (progress > 0.0f)
  27. {
  28. this._downloadProgressSlider.value = progress;
  29. base._statusText.text = progress.ToString("F2");
  30. }
  31. }
  32. protected override void SetupRequest()
  33. {
  34. base.SetupRequest();
  35. // Are there any progress, that we can continue?
  36. this.downloadStartedAt = this.ProcessedBytes;
  37. if (this.downloadStartedAt > 0)
  38. {
  39. // Set the range header
  40. request.SetRangeHeader(this.downloadStartedAt);
  41. }
  42. else
  43. // This is a new request
  44. DeleteKeys();
  45. }
  46. protected override void OnRequestFinished(HTTPRequest req, HTTPResponse resp)
  47. {
  48. base.OnRequestFinished(req, resp);
  49. if (req.State == HTTPRequestStates.Finished && resp.IsSuccess)
  50. DeleteKeys();
  51. }
  52. protected override void OnDownloadProgress(HTTPRequest originalRequest, long downloaded, long downloadLength)
  53. {
  54. double downloadPercent = ((this.downloadStartedAt + downloaded) / (double)this.DownloadLength) * 100;
  55. this._downloadProgressSlider.value = (float)downloadPercent;
  56. this._downloadProgressText.text = string.Format("{0:F1}%", downloadPercent);
  57. }
  58. protected override void ResetProcessedValues()
  59. {
  60. SetDataProcessedUI(this.ProcessedBytes, this.DownloadLength);
  61. }
  62. private float GetSavedProgress()
  63. {
  64. long down = this.ProcessedBytes;
  65. long length = this.DownloadLength;
  66. if (down > 0 && length > 0)
  67. return (down / (float)length) * 100f;
  68. return -1;
  69. }
  70. private void DeleteKeys()
  71. {
  72. PlayerPrefs.DeleteKey(this._downloadPath + ProcessedBytesKey);
  73. PlayerPrefs.DeleteKey(this._downloadPath + DownloadLengthKey);
  74. PlayerPrefs.Save();
  75. }
  76. }
  77. }