UnityWebRequest前后端交互實現(xiàn)過程解析
一、技術(shù)概述
1、描述這個技術(shù)是做什么?
是Unity一套網(wǎng)絡(luò)工具庫,用于進行Http請求
2、學(xué)習(xí)該技術(shù)的原因?
項目需要,防止使用C#原生的網(wǎng)絡(luò)庫,加快開發(fā)速度
3、技術(shù)的難點在哪里
Unity僅提供了基礎(chǔ)的功能,如何把這些功能構(gòu)造成一個能夠穩(wěn)定業(yè)務(wù)開發(fā)的流程是一個比較難處理的問題
二、技術(shù)詳情
描述你是如何實現(xiàn)和使用該技術(shù)的,要求配合代碼和流程圖詳細描述。
HttpCenter類:封裝Get、Post、Put、Delete,維護一個請求隊列
///Get方法例舉 private IEnumerator StartGet(HttpRequest request) { var url = request.Url + "?"; //反射用來填充Url Type type = Type.GetType(request.MsgName); var Msg = Convert.ChangeType(request.Msg, type); PropertyInfo[] properties = Msg.GetType().GetProperties(); for (int i = 0; i < properties.Length; i++) { url += $"{properties[i].Name}={properties[i].GetValue(Msg)}"; if (i != properties.Length - 1) url += "&"; } request.Url = url; using (UnityWebRequest www = UnityWebRequest.Get(request.Url)) { www.certificateHandler = new AcceptAllCertificatesSignedWithASpecificKeyPublicKey(); www.downloadHandler = new DownloadHandlerBuffer(); www.SetRequestHeader("Content-Type", "application/json"); www.SetRequestHeader("token", token); yield return www.SendWebRequest(); DealResult(www, request); }
工程中如何使用:封裝請求、數(shù)據(jù),注冊委托,調(diào)用委托并添加回調(diào)
//部分封裝 public Action<LoginMsg, Action<HttpResponds>> NetLogin; public class LoginMsg : BaseMsg { public LoginMsg(string username, string password) { this.username = username; this.password = password; } public string username { get; set; } public string password { get; set; } } public class HttpResponds { public string data; public RespondsResult Result; public string token; } //注冊委托 AddListener(ref MsgManager.Instance.NetMsgCenter.NetLogin, Method.Post, "User/login"); private void AddListener<T>(ref Action<T,Action<HttpResponds>> registerEvent,Method methodType,string url) where T:BaseMsg { registerEvent += (request, callback) => { HttpRequest httpRequest = new HttpRequest() { Msg = request, HttpMethod = Method.Post, Url = HttpCenter.path + url, Handler = (responds) => { if (responds.Result == RespondsResult.Succ) { try { callback(responds); } catch(Exception ex) { Debug.Log("窗口已銷毀"); if(nowScene == 0) { SceneManager.LoadScene(1); } else { SceneManager.LoadScene(0); } } } } }; HttpCenter.Instance.Send(httpRequest); }; } ///調(diào)用,添加回調(diào) MsgManager.Instance.NetMsgCenter.NetLogin(msg, (responds) => { HttpCenter.Instance.token = responds.token; GetUserMsg userMsg = new GetUserMsg(accountField.text); MsgManager.Instance.NetMsgCenter.NetGetUser(userMsg, (getUserResponds) => { NetDataManager.Instance.user = JsonHelper.DeserializeObject<User>(getUserResponds.data); UIMgr.Instance.CreateFrame("PersonalFrame"); }); });
三、技術(shù)使用中遇到的問題和解決過程
關(guān)于WebRequest中有個奇怪的問題,至今未搞懂,但是有暫時的解決方法。問題是Post方法直接設(shè)置失效,需要先聲明為Put,之后再www.method = UnityWebRequest.kHttpVerbPOST;
四、總結(jié)
主要是基于UnityWebRequest做了一些封裝、利用反射、委托等特性來實現(xiàn)一些基本的功能
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中HttpWebRequest、WebClient、HttpClient的使用詳解
這篇文章主要介紹了C#中HttpWebRequest、WebClient、HttpClient的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12詳解C#如何解決程序卡頓的問題(多線程初步學(xué)習(xí))
在編寫程序的時候,有時候難免會出現(xiàn)后臺運行時間過長的問題,這個時候就要考慮多線程的操作了,所以本文給大家介紹了C#解決程序卡頓問題的方法,需要的朋友可以參考下2024-04-04