HttpTool.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using RootMotion;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. public class HttpTool : Singleton<HttpTool>
  8. {
  9. public void Get(string baseUrl, string methodName, Action<string> callback)
  10. {
  11. StartCoroutine(GetRequestErnieBot(baseUrl, methodName, callback));
  12. }
  13. private IEnumerator GetRequestErnieBot(string baseUrl, string methodName, Action<string> callback)
  14. {
  15. string url = baseUrl + methodName;
  16. Debug.Log(url);
  17. using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
  18. {
  19. yield return webRequest.SendWebRequest();
  20. if (webRequest.error!=null )
  21. {
  22. Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
  23. if (callback != null)
  24. {
  25. callback(null);
  26. }
  27. }
  28. else
  29. {
  30. if (callback != null)
  31. {
  32. callback(webRequest.downloadHandler.text);
  33. }
  34. }
  35. }
  36. }
  37. }