using UnityEngine; using UnityEngine.Networking; using Newtonsoft.Json; using Cysharp.Threading.Tasks; using System.Collections.Generic; /// /// 异步封装unityWebRequest请求 /// public static class AsyncWebReq { /// /// Get /// /// /// 地址 /// 头文件,登录令牌 /// token /// public static async UniTask Get(string url, string _hend = null, string _token = null) { UnityWebRequest getRequest = UnityWebRequest.Get(url); if (_hend != null && _token != null) { getRequest.SetRequestHeader("AddHeader", _hend); getRequest.SetRequestHeader("Hulk-Auth", _token); await getRequest.SendWebRequest(); } else await getRequest.SendWebRequest(); #if UNITY_EDITOR //Debug.Log("async req : " + getRequest.downloadHandler.text); #endif T result = JsonConvert.DeserializeObject(getRequest.downloadHandler.text); getRequest.Dispose(); return result; } /// /// Post /// /// /// 地址 /// /// /// /// public static async UniTask Post(string url, Dictionary _dic, string _hend = null, string _token = null) { WWWForm form = new WWWForm(); foreach (var item in _dic) { form.AddField(item.Key, item.Value); } var postRequest = UnityWebRequest.Post(url, form); if (_hend != null && _token != null) { postRequest.SetRequestHeader("AddHeader", _hend); postRequest.SetRequestHeader("Hulk-Auth", _token); await postRequest.SendWebRequest(); } else await postRequest.SendWebRequest(); #if UNITY_EDITOR //Debug.Log("async req : " + postRequest.downloadHandler.text); #endif T result = JsonConvert.DeserializeObject(postRequest.downloadHandler.text); postRequest.Dispose(); return result; } }