SceneChoose.cs 9.8 KB

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