using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using Blue; using LitJson; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using UnityEngine; using UnityEngine.Networking; public interface IPointService:IService { /// /// 获取点坐标数据 /// /// 后缀 void GetPoint(string methodName, Action callback); /// /// 获取点云文件的下载链接 /// /// 后缀 /// 场景ID /// 文件类型 void GetPointFileDownloadUrl(string methodName, string projectId, PointFileType fileType, Action callback); /// /// 根据点云文件的下载链接去下载点云文件 /// /// 点云文件的下载链接 /// 文件类型 /// 场景ID void DownloadPointFile(string downloadingUrl, PointFileType fileType); void GetRefrencePos(int sceneID); // 获取参照物的坐标旋转 } public class PointService : IPointService { public ScenePosRotInfo PosRot { get; private set; } private string UrlPointCloud; private string UrlObjOfReference; public void OnInit() { //UrlPointCloud = "https://pro.qulivr.com/mr-navigator/v1"; UrlPointCloud = "https://api-fat3.ghz-tech.com/mr-navigator/v1"; UrlObjOfReference = "https://api-fat3.ghz-tech.com/mr-navigator/v1/project/unityReference"; } public void GetPoint(string methodName, Action callback) { CoroutineSystem.Instance.Start_Coroutine(GetPointRequest(methodName, callback)); } public void GetPointFileDownloadUrl(string methodName, string projectId, PointFileType fileType, Action callback) { string url = UrlPointCloud + methodName; // 创建URL UriBuilder uriBuilder = new UriBuilder(url); // 添加查询参数 uriBuilder.Query = "projectId=" + projectId + "&fileType=" + fileType.ToString(); // 创建UnityWebRequest UnityWebRequest webRequest = UnityWebRequest.Get(uriBuilder.Uri); webRequest.SetRequestHeader("Authorization", UserInfo.Instance.Token); // 发送请求 CoroutineSystem.Instance.Start_Coroutine(GetPointGetPointFileRequest(webRequest, callback)); } public void DownloadPointFile(string downloadingUrl, PointFileType fileType) { CoroutineSystem.Instance.Start_Coroutine(DownloadPointFileRequest(downloadingUrl,fileType)); } public void GetRefrencePos(int sceneID) { CoroutineSystem.Instance.Start_Coroutine(GetObjectOfRefrence(UrlObjOfReference,sceneID)); } #region 协程 private IEnumerator GetPointRequest(string methodName, Action callback) { string url = UrlPointCloud + methodName; using (UnityWebRequest webRequest = UnityWebRequest.Get(url)) { webRequest.SetRequestHeader("authorization", UserInfo.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("GetPoint failed: " + webRequest.error + "\n" + webRequest.downloadHandler.text); else callback?.Invoke(webRequest.downloadHandler.text); } } private IEnumerator GetPointGetPointFileRequest(UnityWebRequest webRequest, Action callback) { yield return webRequest.SendWebRequest(); if( webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError) Debug.LogError("GetPointFileDownloadUrl failed: " + webRequest.error); else callback?.Invoke(webRequest.downloadHandler.text); } /// /// 下载点云文件 UnityWebRequest /// /// 下载的Url /// 文件类型 /// private IEnumerator DownloadPointFileRequest(string downloadingUrl, PointFileType fileType) { string fileName = this.GetUtility().GetFileName(downloadingUrl, fileType.ToString()); if (!File.Exists(Application.persistentDataPath + "/Map Data/" + fileName)) { Debug.LogWarning("文件不存在,下载文件"); using (UnityWebRequest webRequestPoint = UnityWebRequest.Get(downloadingUrl)) { webRequestPoint.timeout = 30;//设置超时,若webRequest.SendWebRequest()连接超时会返回,且isNetworkError为true yield return webRequestPoint.SendWebRequest(); if (webRequestPoint.result == UnityWebRequest.Result.ProtocolError || webRequestPoint.result == UnityWebRequest.Result.ConnectionError) Debug.Log("Download Point Cloud Error:" + webRequestPoint.error); else { if (Directory.Exists(Application.persistentDataPath + "/Map Data") == false) // Map Data文件夹不存在 Directory.CreateDirectory(Application.persistentDataPath + "/Map Data"); if (System.IO.File.Exists(Application.persistentDataPath + "/Map Data/" + fileName)) // Map Data/fileName 文件存在则删除 System.IO.File.Delete(Application.persistentDataPath + "/Map Data/" + fileName); var File = webRequestPoint.downloadHandler.data; //获取二进制数据 FileStream nFile = new FileStream(Application.persistentDataPath + "/Map Data/" + fileName, FileMode.Create); //创建文件写入对象 nFile.Write(File, 0, File.Length); //写入数据 nFile.Close(); nFile.Dispose(); nFile = null;//关闭当前流并释放与之相关联的所有系统资源 if (fileType == PointFileType.bytes) this.SendEvent(new LoadMapFileEvent() { fileName = fileName }); } } } else { Debug.LogWarning("文件已存在"); if (fileType == PointFileType.bytes) this.SendEvent(new LoadMapFileEvent() { fileName = fileName }); } } private IEnumerator GetObjectOfRefrence(string url,int sceneID) { using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST")) { ObjofRefrendenceInfo TestGet = new ObjofRefrendenceInfo(); TestGet.id = sceneID; string jsonData = JsonConvert.SerializeObject(TestGet); byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData); webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); webRequest.downloadHandler = new DownloadHandlerBuffer(); webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8"); webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token); yield return webRequest.SendWebRequest(); if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError) Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text); 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)) { // Debug.LogError("message:"+message); ScenePosRotInfo ScenePosRotInfoList = JsonConvert.DeserializeObject(message); PosRot = ScenePosRotInfoList; this.SendEvent(new GetReferencePosEvent() { PosRot = PosRot }); } } } } } } #endregion }