1. Technical Overview
1. Describe what this technology does?
It is a network tool library for Unity, used to make Http requests
2. Why do you learn this technology?
Project needs to prevent the use of C# native network libraries and speed up development
3. What are the difficulties in technology?
Unity only provides basic functions, and how to construct these functions into a process that can stabilize business development is a relatively difficult problem.
2. Technical details
Describe how you implemented and used the technology, requiring detailed descriptions with code and flowcharts.
HttpCenter class: encapsulate Get, Post, Put, Delete, and maintain a request queue
///Get method exampleprivate IEnumerator StartGet(HttpRequest request) { var url = + "?"; //Reflection is used to fill Url Type type = (); var Msg = (, type); PropertyInfo[] properties = ().GetProperties(); for (int i = 0; i < ; i++) { url += $"{properties[i].Name}={properties[i].GetValue(Msg)}"; if (i != - 1) url += "&"; } = url; using (UnityWebRequest www = ()) { = new AcceptAllCertificatesSignedWithASpecificKeyPublicKey(); = new DownloadHandlerBuffer(); ("Content-Type", "application/json"); ("token", token); yield return (); DealResult(www, request); }
How to use in the project: encapsulate requests, data, register delegates, call delegates and add callbacks
// Partial package public Action<LoginMsg, Action<HttpResponds>> NetLogin; public class LoginMsg : BaseMsg { public LoginMsg(string username, string password) { = username; = password; } public string username { get; set; } public string password { get; set; } } public class HttpResponds { public string data; public RespondsResult Result; public string token; } //Registering Entrustment AddListener(ref , , "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 = , Url = + url, Handler = (responds) => { if ( == ) { try { callback(responds); } catch(Exception ex) { ("The window has been destroyed"); if(nowScene == 0) { (1); } else { (0); } } } } }; (httpRequest); }; } ///Call, add callback (msg, (responds) => { = ; GetUserMsg userMsg = new GetUserMsg(); (userMsg, (getUserResponds) => { = <User>(); ("PersonalFrame"); }); });
3. Problems encountered in technology use and solutions
There is a strange problem in WebRequest, which I haven't understood yet, but there is a temporary solution. The problem is that the Post method is set directly ineffective, and it needs to be declared as Put first, and then =;
4. Summary
It mainly uses UnityWebRequest to make some encapsulation, use reflection, delegate and other features to achieve some basic functions.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.