CoroutineHelper.cs 913 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Networking;
  4. namespace ET
  5. {
  6. public static class CoroutineHelper
  7. {
  8. // 有了这个方法,就可以直接await Unity的AsyncOperation了
  9. public static async ETTask GetAwaiter(this AsyncOperation asyncOperation)
  10. {
  11. ETTask task = ETTask.Create(true);
  12. asyncOperation.completed += _ => { task.SetResult(); };
  13. await task;
  14. }
  15. public static async ETTask<string> HttpGet(string link)
  16. {
  17. try
  18. {
  19. UnityWebRequest req = UnityWebRequest.Get(link);
  20. await req.SendWebRequest();
  21. return req.downloadHandler.text;
  22. }
  23. catch (Exception e)
  24. {
  25. throw new Exception($"http request fail: {link.Substring(0,link.IndexOf('?'))}\n{e}");
  26. }
  27. }
  28. }
  29. }