using System; using UnityEngine; using System.Text; using System.Collections; using UnityEngine.Networking; using System.Collections.Generic; namespace SK.Framework { /// /// 网络请求器 /// public class WebRequester : MonoBehaviour { private static WebRequester instance; private WebInterfaceProfile profile; private Dictionary dic; public static WebRequester Instance { get { if(instance == null) { instance = new GameObject("[SKFramework.WebRequest]").AddComponent(); instance.dic = new Dictionary(); instance.profile = Resources.Load("WebInterface Profile"); if(instance.profile == null) { Debug.LogError("加载网络接口配置文件失败"); } DontDestroyOnLoad(instance); } return instance; } } /// /// GET方式调用接口 /// /// 接口地址 /// 回调函数 public static void GET(string url, Action callback) { Instance.StartCoroutine(GetCoroutine(url, callback)); } private static IEnumerator GetCoroutine(string url, Action callback) { using(UnityWebRequest request = UnityWebRequest.Get(url)) { yield return request.SendWebRequest(); //新版本中 已过时 //if (!request.isHttpError && !request.isNetworkError) if (!request.isHttpError && !request.isNetworkError) { callback?.Invoke(request.downloadHandler.text); } else { Debug.LogError(string.Format("GET 调用网络接口{0}失败:{1}", url, request.error)); } } } /// /// POST方式调用接口 /// /// 接口地址 /// POST数据 /// 回调函数 /// 请求头 例如Content-Type=application/json public static void POST(string url, string postData, Action callback, params string[] headers) { Instance.StartCoroutine(PostCoroutine(url, postData, callback, headers)); } private static IEnumerator PostCoroutine(string url, string postData, Action callback, params string[] headers) { using(UnityWebRequest request = UnityWebRequest.Post(url, UnityWebRequest.kHttpVerbPOST)) { if (!string.IsNullOrEmpty(postData)) { byte[] postBytes = Encoding.UTF8.GetBytes(postData); request.uploadHandler = new UploadHandlerRaw(postBytes); } request.downloadHandler = new DownloadHandlerBuffer(); for (int i = 0; i < headers.Length; i++) { string[] kv = headers[i].Split('='); request.SetRequestHeader(kv[0], kv[1]); } yield return request.SendWebRequest(); //新版本中 已过时 //if (!request.isHttpError && !request.isNetworkError) if (!request.isHttpError && !request.isNetworkError) { callback?.Invoke(request.downloadHandler.text); } else { Debug.LogError(string.Format("POST 调用网络接口{0}失败:{1}", url, request.error)); } } } /// /// 注册网络接口 /// /// 网络接口类型 /// 网络接口名称 /// 网络接口 /// 注册成功返回true 否则返回false public static bool RegisterInterface(string webInterfaceName, out T target) where T : AbstractWebInterface { target = null; if (!Instance.dic.TryGetValue(webInterfaceName, out AbstractWebInterface webInterface)) { var info = Array.Find(instance.profile.data, m => m.name == webInterfaceName); if (info != null) { T t = Activator.CreateInstance(); t.name = info.name; t.url = info.url; t.method = info.method; t.args = info.args; Instance.dic.Add(webInterfaceName, t); target = t; return true; } Debug.Log(string.Format("注册网络接口 {0} 失败 - 未找到相关配置信息", webInterfaceName)); return false; } Debug.Log(string.Format("注册网络接口 {0} 失败 - 已经存在", webInterfaceName)); return false; } /// /// 注销网络接口 /// /// 网络接口名称 /// 注销成功返回true 否则返回false public static bool UnregisterInterface(string webInterfaceName) { if (Instance.dic.ContainsKey(webInterfaceName)) { Instance.dic.Remove(webInterfaceName); return true; } Debug.Log(string.Format("注销网络接口 {0} 失败 - 未注册", webInterfaceName)); return false; } /// /// 调用网络接口 发起网络请求 /// /// 网络接口名称 /// 参数 /// 成功发起请求返回true 否则返回false public static bool SendWebRequest(string webInterfaceName, params string[] args) { if (Instance.dic.TryGetValue(webInterfaceName, out var webInterface)) { webInterface.SendWebRequest(args); return true; } Debug.Log(string.Format("调用网络接口 {0} 失败 - 请先进行注册", webInterfaceName)); return false; } } }