EditorHttp.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /****************************************************************************
  2. * Copyright (c) 2020.10 liangxie
  3. *
  4. * https://qframework.cn
  5. * https://github.com/liangxiegame/QFramework
  6. * https://gitee.com/liangxiegame/QFramework
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. ****************************************************************************/
  26. using System;
  27. using UnityEditor;
  28. using UnityEngine;
  29. namespace QFramework
  30. {
  31. public class EditorHttpResponse
  32. {
  33. public ResponseType Type;
  34. public byte[] Bytes;
  35. public string Text;
  36. public string Error;
  37. }
  38. public enum ResponseType
  39. {
  40. SUCCEED,
  41. EXCEPTION,
  42. TIMEOUT,
  43. }
  44. public static class EditorHttp
  45. {
  46. public class EditorWWWExecuter
  47. {
  48. private WWW mWWW;
  49. private Action<EditorHttpResponse> mResponse;
  50. private Action<float> mOnProgress;
  51. private bool mDownloadMode;
  52. public EditorWWWExecuter(WWW www, Action<EditorHttpResponse> response, Action<float> onProgress = null,
  53. bool downloadMode = false)
  54. {
  55. mWWW = www;
  56. mResponse = response;
  57. mOnProgress = onProgress;
  58. mDownloadMode = downloadMode;
  59. // EditorApplication.update += Update;
  60. }
  61. void Update()
  62. {
  63. if (mWWW != null && mWWW.isDone)
  64. {
  65. if (string.IsNullOrEmpty(mWWW.error))
  66. {
  67. if (mDownloadMode)
  68. {
  69. if (mOnProgress != null)
  70. {
  71. mOnProgress(1.0f);
  72. }
  73. mResponse(new EditorHttpResponse()
  74. {
  75. Type = ResponseType.SUCCEED,
  76. Bytes = mWWW.bytes
  77. });
  78. }
  79. else
  80. {
  81. mResponse(new EditorHttpResponse()
  82. {
  83. Type = ResponseType.SUCCEED,
  84. Text = mWWW.text
  85. });
  86. }
  87. }
  88. else
  89. {
  90. mResponse(new EditorHttpResponse()
  91. {
  92. Type = ResponseType.EXCEPTION,
  93. Error = mWWW.error
  94. });
  95. }
  96. Dispose();
  97. }
  98. if (mWWW != null && mDownloadMode)
  99. {
  100. if (mOnProgress != null)
  101. {
  102. mOnProgress(mWWW.progress);
  103. }
  104. }
  105. }
  106. void Dispose()
  107. {
  108. mWWW.Dispose();
  109. mWWW = null;
  110. // EditorApplication.update -= Update;
  111. }
  112. }
  113. public static void Get(string url, Action<EditorHttpResponse> response)
  114. {
  115. new EditorWWWExecuter(new WWW(url), response);
  116. }
  117. public static void Post(string url, WWWForm form, Action<EditorHttpResponse> response)
  118. {
  119. new EditorWWWExecuter(new WWW(url, form), response);
  120. }
  121. public static void Download(string url, Action<EditorHttpResponse> response, Action<float> onProgress = null)
  122. {
  123. new EditorWWWExecuter(new WWW(url), response, onProgress, true);
  124. }
  125. }
  126. }