SceneChoose.cs 11 KB

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