[Unity网络]Http通信

参考链接:

https://www.jianshu.com/p/a2b518ad53f7

https://www.jianshu.com/p/c6773e431bb5

https://www.jianshu.com/p/3da9191f82a0

 

HttpManager.cs

  1 using System;
  2 using System.Collections;
  3 using UnityEngine;
  4 using UnityEngine.Networking;
  5 
  6 public class HttpManager : MonoSingletion<HttpManager>
  7 {
  8     //Get请求(获取信息)
  9     public void Get(string url, Action<string> action)
 10     {
 11         StartCoroutine(_Get(url, action));
 12     }
 13 
 14     IEnumerator _Get(string url, Action<string> action)
 15     {
 16         UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
 17         yield return unityWebRequest.SendWebRequest();
 18         if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
 19         {
 20             Debug.LogError("url:" + url + " error:" + unityWebRequest.error);
 21         }
 22         else
 23         {
 24             if (action != null)
 25             {
 26                 action(unityWebRequest.downloadHandler.text);
 27             }
 28         }
 29     }
 30 
 31     //Post请求(上传信息)
 32     public void Post(string url, WWWForm wWWForm, Action<string> action)
 33     {
 34         StartCoroutine(_Post(url, wWWForm, action));
 35     }
 36 
 37     IEnumerator _Post(string url, WWWForm wWWForm, Action<string> action)
 38     {
 39         //wWWForm = new WWWForm();
 40         //wWWForm.AddField("qwe", "123");
 41         UnityWebRequest unityWebRequest = UnityWebRequest.Post(url, wWWForm);
 42         yield return unityWebRequest.SendWebRequest();
 43         if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
 44         {
 45             Debug.LogError("url:" + url + " error:" + unityWebRequest.error);
 46         }
 47         else
 48         {
 49             if (action != null)
 50             {
 51                 action(unityWebRequest.downloadHandler.text);
 52             }
 53         }
 54     }
 55 
 56     //请求Texture
 57     public void RequestTexture(string url, Action<Texture2D> action)
 58     {
 59         StartCoroutine(_RequestTexture(url, action));
 60     }
 61 
 62     IEnumerator _RequestTexture(string url, Action<Texture2D> action)
 63     {
 64         UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(url);
 65         yield return unityWebRequest.SendWebRequest();
 66         if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
 67         {
 68             Debug.LogError("url:" + url + " error:" + unityWebRequest.error);
 69         }
 70         else
 71         {
 72             if (action != null)
 73             {
 74                 action(DownloadHandlerTexture.GetContent(unityWebRequest));
 75             }
 76         }
 77     }
 78 
 79     //请求AudioClip
 80     public void RequestAudioClip(string url, AudioType audioType, Action<AudioClip> action)
 81     {
 82         StartCoroutine(_RequestAudioClip(url, audioType, action));
 83     }
 84 
 85     IEnumerator _RequestAudioClip(string url, AudioType audioType, Action<AudioClip> action)
 86     {
 87         UnityWebRequest unityWebRequest = UnityWebRequestMultimedia.GetAudioClip(url, audioType);
 88         yield return unityWebRequest.SendWebRequest();
 89         if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
 90         {
 91             Debug.LogError("url:" + url + " error:" + unityWebRequest.error);
 92         }
 93         else
 94         {
 95             if (action != null)
 96             {
 97                 action(DownloadHandlerAudioClip.GetContent(unityWebRequest));
 98             }
 99         }
100     }
101 
102     //请求AssetBundle
103     public void RequestAssetBundle(string url, Action<AssetBundle> action)
104     {
105         StartCoroutine(_RequestAssetBundle(url, action));
106     }
107 
108     IEnumerator _RequestAssetBundle(string url, Action<AssetBundle> action)
109     {
110         UnityWebRequest unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(url, 0);
111         yield return unityWebRequest.SendWebRequest();
112         if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
113         {
114             Debug.LogError("url:" + url + " error:" + unityWebRequest.error);
115         }
116         else
117         {
118             if (action != null)
119             {
120                 action(DownloadHandlerAssetBundle.GetContent(unityWebRequest));
121             }
122         }
123     }
124 }

 

上一篇:.Net Core WebApi 自定义资源过滤器(使用缓存)


下一篇:Mutation 和 Action 的差异