SceneChoose.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. using LitJson;
  2. using Newtonsoft.Json.Linq;
  3. using SC.XR.Unity;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using Newtonsoft.Json;
  11. using Blue;
  12. using UnityEngine.Networking;
  13. public class SceneChoose : BaseUI
  14. {
  15. private Button m_LeftBtn;
  16. private Button m_RightBtn;
  17. private Button m_OkBtn;
  18. private GameObject m_SceneItem;
  19. private Transform m_ScenePanel;
  20. /// <summary>
  21. /// 场景选择面板中确认的场景
  22. /// </summary>
  23. private SceneValue m_CurrentData;
  24. private List<SChooseItem> m_Items;
  25. private CarouselImage m_CarImage;
  26. private List<SceneValue> m_SceneData;
  27. protected override void OnAwake()
  28. {
  29. base.OnAwake();
  30. m_SceneData = new List<SceneValue>();
  31. m_Items = new List<SChooseItem>();
  32. m_CurrentData = null;
  33. m_LeftBtn = CacheTransform.Find("Left").GetComponent<Button>();
  34. m_RightBtn = CacheTransform.Find("Right").GetComponent<Button>();
  35. m_OkBtn = CacheTransform.Find("OK").GetComponent<Button>();
  36. m_SceneItem = CacheTransform.Find("SceneItem").gameObject;
  37. m_ScenePanel = CacheTransform.Find("ScenePanel");
  38. m_ScenePanel.GetComponent<RectTransform>().sizeDelta = new Vector2(400, 150);
  39. m_LeftBtn.onClick.AddListener(ONClickLeftBtn);
  40. m_RightBtn.onClick.AddListener(ONClickRightBtn);
  41. m_OkBtn.onClick.AddListener(OnClickOKBtn);
  42. }
  43. #region UI监听事件
  44. private void ONClickLeftBtn()
  45. {
  46. if (m_CarImage != null)
  47. {
  48. m_CarImage.MoveToIndex(m_CarImage.CurrentIndex + 1);
  49. }
  50. }
  51. private void ONClickRightBtn()
  52. {
  53. if (m_CarImage != null)
  54. {
  55. m_CarImage.MoveToIndex(m_CarImage.CurrentIndex - 1);
  56. }
  57. }
  58. private void OnClickOKBtn()
  59. {
  60. if (m_CurrentData != null)
  61. {
  62. //根据场景数据,下载资源
  63. Debug.Log("选择的场景为:" + m_CurrentData.name);
  64. SendSceneDetail detail = new SendSceneDetail();
  65. detail.id = m_CurrentData.id;
  66. string str = JsonConvert.SerializeObject(detail);
  67. //获取账号下的点数据
  68. HttpTool.Instance.GetPoint("/project/position/" + detail.id.ToString(), message =>
  69. {
  70. if (!string.IsNullOrWhiteSpace(message))
  71. {
  72. JObject jobject = JObject.Parse(message);
  73. if (jobject["code"].ToString() == "200")
  74. {
  75. message = jobject["data"].ToString();
  76. if (!string.IsNullOrWhiteSpace(message))
  77. {
  78. List<PointJsonInfo> listScenesPage = JsonConvert.DeserializeObject<List<PointJsonInfo>>(message);
  79. this.SendCommand(new GetPointPosSuccessCommand(listScenesPage));
  80. return;
  81. }
  82. }
  83. }
  84. });
  85. DownLoadFile(detail.id,"json");
  86. DownLoadFile(detail.id,"bytes");
  87. HttpTool.Instance.PostTest("/project/detail", str, (mes) =>
  88. {
  89. JObject jObject = JObject.Parse(mes);
  90. if (jObject["code"].ToString() == "200")
  91. {
  92. mes = jObject["data"].ToString();
  93. SceneValue scene = JsonConvert.DeserializeObject<SceneValue>(mes);
  94. GameManager.Instance.MapPicUrl = scene.backgroundImages[0].base64;
  95. GameManager.Instance.GetMinMap(scene);
  96. GetSNInit();
  97. }
  98. else
  99. {
  100. UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
  101. Module_Notice.getInstance.SetNoticeInfo("场景地图获取失败", "请至网页端检查场景数据", NoticeType.Normal, 1.7f);
  102. Module_Notice.getInstance._Follower.WindowAnchor = TextAnchor.UpperCenter;
  103. Module_Notice.getInstance.StartNotice(3f);
  104. UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
  105. }
  106. });
  107. }
  108. else
  109. {
  110. ///Module_Notice.getInstance.SetNoticeInfo("未选择导览场景", "请先勾选要进入的场景", NoticeType.Normal, 1.7f);
  111. //Module_Notice.getInstance._Follower.WindowAnchor = TextAnchor.UpperCenter;
  112. //Module_Notice.getInstance.StartNotice(3f);
  113. this.SendCommand(new InstantiateCommand(InstantiateSystem.Instance.BlueObject.WarningPopUp));
  114. }
  115. }
  116. private void DownLoadFile(int projectId,string fileType)
  117. {
  118. //获取账号下的点云文件数据
  119. HttpTool.Instance.GetPointFile("/project/pointcloud", projectId.ToString(),fileType, message=>
  120. {
  121. if (!string.IsNullOrWhiteSpace(message))
  122. {
  123. JObject jobject = JObject.Parse(message);
  124. if (jobject["code"].ToString() == "200")
  125. {
  126. message = jobject["data"].ToString();
  127. if (!string.IsNullOrWhiteSpace(message))
  128. {
  129. PointFileData PointFile = JsonConvert.DeserializeObject<PointFileData>(message);
  130. //Debug.LogError(PointFile.url);
  131. this.SendCommand<DownloadFileCommand>(new DownloadFileCommand(PointFile.url,fileType,projectId){});
  132. }
  133. }
  134. }
  135. });
  136. }
  137. private void GetSNInit()
  138. {
  139. // 选择场景
  140. SendInit send = new SendInit();
  141. send.sn = SendSN.GetSN();
  142. //send.sn = API_GSXR_Slam.SlamManager.plugin.SN;
  143. send.projectId = m_CurrentData.id;
  144. string jsonString = JsonConvert.SerializeObject(send);
  145. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.sceneChoose);
  146. //通过SN获取场景数据
  147. HttpTool.Instance.PostTest("/sn/init", jsonString, (message) =>
  148. {
  149. Debug.Log("SN:" + send.sn);
  150. Debug.Log("通过SN获取场景数据:" + message);
  151. if (!string.IsNullOrWhiteSpace(message))
  152. {
  153. JObject jobject = JObject.Parse(message);
  154. if (jobject.GetValue("code") != null)
  155. {
  156. if (jobject["code"].ToString() == "400")
  157. {
  158. UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
  159. //Module_Notice.getInstance.SetNoticeInfo("场景数据获取失败", jobject["data"].ToString(), NoticeType.Normal, 1.7f);
  160. //Module_Notice.getInstance._Follower.WindowAnchor = TextAnchor.UpperCenter;
  161. //Module_Notice.getInstance.StartNotice(3f);
  162. }
  163. }
  164. else
  165. {
  166. message = jobject["data"].ToString();
  167. GameManager.Instance.text.text = message;
  168. // SceneValue scene = JsonMapper.ToObject<SceneValue>(message);
  169. SceneValue scene = JsonConvert.DeserializeObject<SceneValue>(message);
  170. DataManager.Instance.CurrentScene = scene;
  171. DataManager.Instance.ProjectID = m_CurrentData.id;
  172. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.createScene);
  173. StartCoroutine(GetSpoitValueDetail(scene));
  174. GameManager.Instance.text.text = JsonConvert.SerializeObject(scene);
  175. Debug.Log("当前选择场景数据: " + JsonConvert.SerializeObject(scene));
  176. }
  177. }
  178. else
  179. {
  180. UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
  181. }
  182. });
  183. }
  184. private IEnumerator GetSpoitValueDetail(SceneValue scene)
  185. {
  186. var spoits = scene.listSpoit;
  187. for (int i = 0; i < spoits.Count; i++)
  188. {
  189. //通过ID获取场景详情
  190. SendSceneDetail detail = new SendSceneDetail();
  191. detail.id = spoits[i].id;
  192. string jsonString = JsonConvert.SerializeObject(detail);
  193. yield return StartCoroutine(HttpTool.Instance.PostRequestSpoitDetail("/viewpoint/detail", jsonString, (message) =>
  194. {
  195. Debug.Log("当前选择场景数据: " + message);
  196. JObject jObject = JObject.Parse(message);
  197. if (jObject["code"].ToString() == "200")
  198. {
  199. message = jObject["data"].ToString();
  200. SpoitValueDetail spoit = JsonConvert.DeserializeObject<SpoitValueDetail>(message);
  201. GameManager.Instance.SpoitsValueDetail.Add(spoit.id, spoit);
  202. }
  203. }));
  204. }
  205. Debug.Log(scene.vuforiaXML);
  206. string xmlName = Path.GetFileName(scene.vuforiaXML);
  207. if (xmlName != null)
  208. {
  209. xmlName = xmlName.Substring(0, xmlName.Length - 4);
  210. AppConfigConst.IMAGE_TARGET_FILE_NAME = xmlName;
  211. }
  212. if (scene.vuforiaXML != null)
  213. {
  214. DownloadData xml = new DownloadData();
  215. string filename = Path.GetFileName(scene.vuforiaXML);
  216. xml.name = filename;
  217. xml.type = 8;
  218. xml.downloadPath = scene.vuforiaXML;
  219. xml.updateTime = (int)scene.updateTime;
  220. xml.localSavePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + filename;
  221. DownloadManager.Instance.AddDownloadData(xml);
  222. DownloadData dat = new DownloadData();
  223. string filename2 = Path.GetFileName(scene.vuforiaDat);
  224. dat.name = filename2;
  225. dat.type = 8;
  226. dat.downloadPath = scene.vuforiaDat;
  227. dat.updateTime = (int)scene.updateTime;
  228. dat.localSavePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + filename2;
  229. DownloadManager.Instance.AddDownloadData(dat);
  230. }
  231. yield return new WaitForSeconds(1f);
  232. //创建场景
  233. GameManager.Instance.CreateScene(scene);
  234. Hide();
  235. }
  236. private void UpdateSceneItem()
  237. {
  238. if (m_SceneData == null)
  239. {
  240. return;
  241. }
  242. ClearItem();
  243. for (int i = 0; i < m_SceneData.Count; i++)
  244. {
  245. var obj = Instantiate(m_SceneItem);
  246. obj.transform.SetParent(m_ScenePanel);
  247. var item = obj.AddComponent<SChooseItem>();
  248. obj.gameObject.SetActive(true);
  249. obj.transform.localScale = new Vector3(1, 1, 1);
  250. obj.transform.localEulerAngles = Vector3.zero;
  251. item.OnSelectScene += OnClickSceneItem;
  252. item.SceneData = m_SceneData[i];
  253. item.Init();
  254. m_Items.Add(item);
  255. }
  256. if (m_CarImage == null)
  257. {
  258. m_CarImage = m_ScenePanel.gameObject.AddComponent<CarouselImage>();
  259. }
  260. m_CarImage.mCellSize = new Vector2(400, 150);
  261. m_CarImage.mSpacing = new Vector2(0, 0);
  262. m_CarImage.mAutoLoop = false;
  263. m_CarImage.mDrag = true;
  264. m_CarImage.mLoopSpaceTime = 5f;
  265. m_CarImage.resizeChildren();
  266. }
  267. private void OnClickSceneItem(SceneValue item, bool b)
  268. {
  269. m_CurrentData = b ? item : null;
  270. }
  271. #endregion
  272. protected override void OnInit()
  273. {
  274. base.OnInit();
  275. }
  276. protected override void OnShow(object param)
  277. {
  278. base.OnShow(param);
  279. m_SceneData = (List<SceneValue>)param;
  280. UpdateSceneItem();
  281. }
  282. protected override void OnHide()
  283. {
  284. base.OnHide();
  285. }
  286. /// <summary>
  287. /// 清除所有场景信息
  288. /// </summary>
  289. private void ClearItem()
  290. {
  291. while (m_Items.Count > 0)
  292. {
  293. var item = m_Items[0];
  294. m_Items.RemoveAt(0);
  295. Destroy(item.gameObject);
  296. }
  297. }
  298. }