using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; public static class InterfaceManager { public static string IP { get => "111.229.30.246:"; } public static string Port { get => "48888"; } public static string IpAddress { get => string.Format("http://{0}:{1}", IP, Port); } #region 接口 /// /// 想定Wbe接口 /// public static string Url_Action { get => IpAddress + "/Handler/Thinkingfile.ashx?action=all"; } /// /// 修改想定设备参数 /// public static string Url_Updatedevicepara { get => IpAddress + "/Handler/Thinkingfile.ashx?action=updatedevicepara"; } /// /// 查询想定设备参数 /// public static string Url_Querydevicepara { get => IpAddress + "/Handler/Thinkingfile.ashx?action=querydevicepara&"; } /// /// 查询想定设备 /// public static string Url_Querydevice { get => IpAddress + "/Handler/Thinkingfile.ashx?action=querydevice&"; } /// /// 添加设备 /// public static string Url_Adddevice { get => IpAddress + "/Handler/Thinkingfile.ashx?action=adddevice"; } /// /// 添加设备参数 /// public static string Url_Adddevicepara { get => IpAddress + "/Handler/Thinkingfile.ashx?action=adddevicepara"; } /// /// 修改想定接口 /// public static string Url_updatedevice { get => IpAddress + "/Handler/Thinkingfile.ashx?action=updatedevice"; } /// /// 添加模型路径参数接口 /// public static string Url_updatecontent { get => IpAddress + "/Handler/Thinkingfile.ashx?action=updatecontent"; } /// /// 修改环境 /// public static string Url_update_env { get => IpAddress + "/Handler/Thinkingfile.ashx?action=update_env"; } #endregion public static string GetLocalTxt(string path) { using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { return reader.ReadToEnd(); } } public static IEnumerator GetBytes(string url, Action callback) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { callback(null); Debug.Log("网络请求失败了"); } else { if (!string.IsNullOrEmpty(www.downloadHandler.text)) { callback(www.downloadHandler.text); } else { callback(null); } } } } public static IEnumerator GetString(string url, Action callback) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { callback(null); Debug.Log("网络请求失败了"); } else { if (!string.IsNullOrEmpty(www.downloadHandler.text)) { callback(www.downloadHandler.text); } else { callback(null); } } } } public static IEnumerator GetString(string url, int time, Action callback) { while (true) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口 yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { callback(null); Debug.Log("网络请求失败了"); } else { if (!string.IsNullOrEmpty(www.downloadHandler.text)) { callback(www.downloadHandler.text); } else { callback(null); } } } } } public static IEnumerator PostString(string url, WWWForm formdata, Dictionary headers = null, Action callback = null) { using (UnityWebRequest www = UnityWebRequest.Post(url, formdata)) { foreach (var item in headers) { www.SetRequestHeader(item.Key, item.Value); } yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { callback(null); Debug.Log("网络请求失败了"); } else { if (!string.IsNullOrEmpty(www.downloadHandler.text)) { callback(www.downloadHandler.text); } else { callback(null); } } } } public static IEnumerator GetString(string url, Dictionary headers = null,int time=50,Action callback = null) { while (true) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口 foreach (var item in headers) { www.SetRequestHeader(item.Key, item.Value); } yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { callback(null); Debug.Log("网络请求失败了"); } else { if (!string.IsNullOrEmpty(www.downloadHandler.text)) { callback(www.downloadHandler.text); } else { callback(null); } } } } } public static IEnumerator GetString(string url, Dictionary headers = null, Action callback = null) { using (UnityWebRequest www = UnityWebRequest.Get(url)) { foreach (var item in headers) { www.SetRequestHeader(item.Key, item.Value); } yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { callback(null); Debug.Log("网络请求失败了"); } else { if (!string.IsNullOrEmpty(www.downloadHandler.text)) { callback(www.downloadHandler.text); } else { callback(null); } } } } }