using System; using System.Collections; using System.IO; using UnityEngine; using UnityEngine.Networking; namespace GeneralTools { public class WebRequestManager : SingletonBaseAttribute { #if UNITY_2017_1_OR_NEWER public void Get(string url, Action actionResult) { StartCoroutine(_Get(url, actionResult)); } public void DownloadFile(string url, string downloadFilePathAndName, Action actionResult) { StartCoroutine(_DownloadFile(url, downloadFilePathAndName, actionResult)); } public void GetTexture(string url, Action actionResult) { StartCoroutine(_GetTexture(url, actionResult)); } public void GetAssetBundle(string url, Action actionResult) { StartCoroutine(_GetAssetBundle(url, actionResult)); } public void GetAudioClip(string url, Action actionResult, AudioType audioType = AudioType.WAV) { StartCoroutine(_GetAudioClip(url, actionResult, audioType)); } public void Post(string serverURL, WWWForm wWWForm, Action actionResult) { StartCoroutine(_Post(serverURL, wWWForm, actionResult)); } /// /// 通过PUT方式将字节流传到服务器 /// /// 需要上传的字节流 /// 处理返回结果的委托 /// public void UploadByPut(string url, byte[] contentBytes, Action actionResult) { StartCoroutine(_UploadByPut(url, contentBytes, actionResult, "")); } /// /// GET请求 /// /// 请求发起后处理回调结果的委托 /// IEnumerator _Get(string url, Action actionResult) { using (UnityWebRequest uwr = UnityWebRequest.Get(url)) { yield return uwr.SendWebRequest(); #if UNITY_2019 || UNITY_2018||UNITY_2017 if (uwr.isNetworkError || uwr.isHttpError) #else if (uwr.result == UnityWebRequest.Result.ConnectionError || uwr.result == UnityWebRequest.Result.ProtocolError) #endif { Debug.LogError(uwr.error); } else { actionResult?.Invoke(uwr); } } } /// 下载文件 UnityWebRequest uwr; IEnumerator _DownloadFile(string url, string downloadFilePathAndName, Action actionResult) { uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET); uwr.downloadHandler = new DownloadHandlerFile(downloadFilePathAndName); yield return uwr.SendWebRequest(); #if UNITY_2019 || UNITY_2018||UNITY_2017 if (uwr.isNetworkError || uwr.isHttpError) #else if (uwr.result == UnityWebRequest.Result.ConnectionError || uwr.result == UnityWebRequest.Result.ProtocolError) #endif { Debug.LogError(uwr.error); } else { actionResult?.Invoke(uwr); } } protected void Update() { if (uwr != null) { Debug.Log(uwr.downloadProgress); } } /// 请求图片 IEnumerator _GetTexture(string url, Action actionResult) { UnityWebRequest uwr = new UnityWebRequest(url); DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true); uwr.downloadHandler = downloadTexture; yield return uwr.SendWebRequest(); Texture2D t = null; #if UNITY_2019 || UNITY_2018||UNITY_2017 if (uwr.isNetworkError || uwr.isHttpError) #else if (uwr.result == UnityWebRequest.Result.ConnectionError || uwr.result == UnityWebRequest.Result.ProtocolError) #endif { Debug.LogError(uwr.error); } else { t = downloadTexture.texture; } actionResult?.Invoke(t, url); } /// 请求AssetBundle IEnumerator _GetAssetBundle(string url, Action actionResult) { UnityWebRequest www = new UnityWebRequest(url); DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(www.url, uint.MaxValue); www.downloadHandler = handler; yield return www.SendWebRequest(); AssetBundle bundle = null; #if UNITY_2019 || UNITY_2018||UNITY_2017 if (www.isNetworkError || www.isHttpError) #else if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError) #endif { Debug.LogError(www.error); } else { bundle = handler.assetBundle; } actionResult?.Invoke(bundle, url); } //音效 IEnumerator _GetAudioClip(string url, Action actionResult, AudioType audioType = AudioType.WAV) { using (var uwr = UnityWebRequestMultimedia.GetAudioClip(url, audioType)) { yield return uwr.SendWebRequest(); #if UNITY_2019 || UNITY_2018||UNITY_2017 if (uwr.isNetworkError || uwr.isHttpError) #else if (uwr.result == UnityWebRequest.Result.ConnectionError || uwr.result == UnityWebRequest.Result.ProtocolError) #endif { Debug.LogError(uwr.error); } else { actionResult?.Invoke(DownloadHandlerAudioClip.GetContent(uwr), url); } } } /// 向服务器提交post请求 IEnumerator _Post(string serverURL, WWWForm wWWForm, Action actionResult) { UnityWebRequest uwr = UnityWebRequest.Post(serverURL, wWWForm); yield return uwr.SendWebRequest(); #if UNITY_2019 || UNITY_2018||UNITY_2017 if (uwr.isNetworkError || uwr.isHttpError) #else if (uwr.result == UnityWebRequest.Result.ConnectionError || uwr.result == UnityWebRequest.Result.ProtocolError) #endif { Debug.LogError(uwr.error); } else { actionResult?.Invoke(uwr); } } /// /// 通过PUT方式将字节流传到服务器 /// /// 需要上传的字节流 /// 处理返回结果的委托 /// 设置header文件中的Content-Type属性 /// IEnumerator _UploadByPut(string url, byte[] contentBytes, Action actionResult, string contentType = "application/octet-stream") { UnityWebRequest uwr = new UnityWebRequest(); UploadHandler uploader = new UploadHandlerRaw(contentBytes); uploader.contentType = contentType; uwr.uploadHandler = uploader; yield return uwr.SendWebRequest(); bool res = true; #if UNITY_2019 || UNITY_2018||UNITY_2017 if (uwr.isNetworkError || uwr.isHttpError) #else if (uwr.result == UnityWebRequest.Result.ConnectionError || uwr.result == UnityWebRequest.Result.ProtocolError) #endif { Debug.LogError(uwr.error); res = false; } actionResult?.Invoke(res); } #endif } }