PDFWebRequest.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if UNITY_2018_3_OR_NEWER
  2. #define USE_UNITYWEBREQUEST
  3. #endif
  4. #if USE_UNITYWEBREQUEST
  5. using UnityEngine.Networking;
  6. #else
  7. using UnityEngine;
  8. #endif
  9. using System;
  10. using System.Collections;
  11. namespace Paroxe.PdfRenderer
  12. {
  13. /// <summary>
  14. /// WWW is deprecated in recent version of Unity but it's not available on older version like Unity 5.3
  15. /// This class only acts like a shim supporting the right implementation depending on which version of
  16. /// Unity being use.
  17. /// </summary>
  18. public sealed class PDFWebRequest
  19. #if USE_UNITYWEBREQUEST
  20. : UnityWebRequest, IEnumerator
  21. #else
  22. : IDisposable, IEnumerator
  23. #endif
  24. {
  25. #if !USE_UNITYWEBREQUEST
  26. private WWW m_WWW;
  27. #endif
  28. public PDFWebRequest(string url)
  29. #if USE_UNITYWEBREQUEST
  30. : base(url)
  31. #endif
  32. {
  33. #if USE_UNITYWEBREQUEST
  34. downloadHandler = new DownloadHandlerBuffer();
  35. disposeDownloadHandlerOnDispose = true;
  36. #else
  37. m_WWW = new WWW(url);
  38. #endif
  39. }
  40. #if USE_UNITYWEBREQUEST
  41. public float progress
  42. {
  43. get { return downloadProgress; }
  44. }
  45. public byte[] bytes
  46. {
  47. get { return downloadHandler.data; }
  48. }
  49. object IEnumerator.Current
  50. {
  51. get { return null; }
  52. }
  53. bool IEnumerator.MoveNext()
  54. {
  55. return !isDone;
  56. }
  57. void IEnumerator.Reset()
  58. {
  59. throw new NotImplementedException();
  60. }
  61. #else
  62. public float progress
  63. {
  64. get { return m_WWW.progress; }
  65. }
  66. public byte[] bytes
  67. {
  68. get { return m_WWW.bytes; }
  69. }
  70. public string error
  71. {
  72. get { return m_WWW.error; }
  73. }
  74. public bool isDone
  75. {
  76. get { return m_WWW.isDone; }
  77. }
  78. object IEnumerator.Current
  79. {
  80. get { return null; }
  81. }
  82. bool IEnumerator.MoveNext()
  83. {
  84. return !isDone;
  85. }
  86. void IEnumerator.Reset()
  87. {
  88. throw new NotImplementedException();
  89. }
  90. #endif
  91. #if !USE_UNITYWEBREQUEST
  92. public void SendWebRequest() { }
  93. public void Dispose()
  94. {
  95. m_WWW.Dispose();
  96. }
  97. #endif
  98. }
  99. }