using Blue; using LitJson; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.Networking; public interface IUpOrDownloadService : IService { List PosRot { get; } string URL { get; } void GetScenePosRot(string Url); void UploadScenePosRot(string Url, string JsonString); } public class UpOrDownloadService : IUpOrDownloadService { public List PosRot { get; private set; } public string URL { get; private set; } public void OnInit() { } void IUpOrDownloadService.GetScenePosRot(string Url) { RootSystem.Instance.StartCoroutine(GetScenePosRotRequest(Url)); } void IUpOrDownloadService.UploadScenePosRot(string Url, string JsonString) { RootSystem.Instance.StartCoroutine(RequestUpload(Url,JsonString)); } private IEnumerator GetScenePosRotRequest(string Url) { using (UnityWebRequest webRequest = UnityWebRequest.Get(Url)) { webRequest.SetRequestHeader("authorization", HttpTool.Instance.Token); //??header foreach (var v in HttpTool.Instance.RequestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError) Debug.LogError("UnityWebRequest Error"); else { if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text)) { string message = webRequest.downloadHandler.text; JObject jobject = JObject.Parse(message); if (jobject["code"].ToString() == "200") { message = jobject["data"].ToString(); if (!string.IsNullOrWhiteSpace(message)) { List ScenePosRotInfoList = JsonConvert.DeserializeObject>(message); PosRot = ScenePosRotInfoList; URL = Url; this.TriggerEvent(new GetScenePosRotEvent() { PosRot = PosRot }); } } } } } } public IEnumerator RequestUpload(string Url, string jsonString) { using (UnityWebRequest webRequest = new UnityWebRequest(Url, "POST")) { JsonData data = JsonMapper.ToObject(jsonString); JsonData d2 = new JsonData(); d2["position"] = data; byte[] bodyRaw = Encoding.UTF8.GetBytes(d2.ToJson()); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); webRequest.downloadHandler = new DownloadHandlerBuffer(); webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8"); webRequest.SetRequestHeader("authorization", HttpTool.Instance.Token); foreach (var v in HttpTool.Instance.RequestHeader) { webRequest.SetRequestHeader(v.Key, v.Value); } yield return webRequest.SendWebRequest(); if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError) { Debug.LogError(webRequest.downloadHandler.error); InstantiateCommand Command = new InstantiateCommand( InstantiateSystem.Instance.BlueObject.WarningPopUp, InstantiateSystem.Instance.BlueObject.NetErrorText); CommandExtensionSystem.Instance.Send(Command); } else { Debug.LogError(webRequest.downloadHandler.text); if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text)) { string message = webRequest.downloadHandler.text; JObject jobject = JObject.Parse(message); if (jobject["code"].ToString() == "200") { message = jobject["data"].ToString(); if (!string.IsNullOrWhiteSpace(message)) { List ScenePosRotInfoList = JsonConvert.DeserializeObject>(message); PosRot = ScenePosRotInfoList; } } } InstantiateCommand Command = new InstantiateCommand( InstantiateSystem.Instance.BlueObject.WarningPopUp, InstantiateSystem.Instance.BlueObject.SuccessText); CommandExtensionSystem.Instance.Send(Command); } } } }