SceneChoose.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. this.GetModel<ISceneModel>().sceneID = m_CurrentData.id;
  68. this.SendCommand(new PointFileGetUrlCommand(detail.id, PointFileType.json));
  69. this.SendCommand(new PointFileGetUrlCommand(detail.id, PointFileType.bytes));
  70. this.SendCommand(new PointPosRotDownloadCommand(detail.id));
  71. this.SendCommand(new GetReferencePosCommand(detail.id));
  72. this.SendCommand(new MinMapGetSetCommand());
  73. HttpTool.Instance.PostTest("/project/detail", str, (mes) =>
  74. {
  75. if (mes == "UnityWebRequest Error") return;
  76. JObject jObject = JObject.Parse(mes);
  77. if (jObject["code"].ToString() == "200")
  78. {
  79. mes = jObject["data"].ToString();
  80. SceneValue scene = JsonConvert.DeserializeObject<SceneValue>(mes);
  81. Debug.Log("scene.backgroundImages===>" );
  82. Debug.Log("scene.backgroundImages===>"+ scene.backgroundImages.Count);
  83. GameManager.Instance.MapPicUrl = scene.backgroundImages[0].base64;
  84. GameManager.Instance.GetMinMap(scene);
  85. GetSNInit();
  86. }
  87. else
  88. {
  89. UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
  90. Module_Notice.getInstance.SetNoticeInfo("场景地图获取失败", "请至网页端检查场景数据", NoticeType.Normal, 1.7f);
  91. Module_Notice.getInstance._Follower.WindowAnchor = TextAnchor.UpperCenter;
  92. Module_Notice.getInstance.StartNotice(3f);
  93. UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
  94. }
  95. });
  96. }
  97. else
  98. {
  99. ///Module_Notice.getInstance.SetNoticeInfo("未选择导览场景", "请先勾选要进入的场景", NoticeType.Normal, 1.7f);
  100. //Module_Notice.getInstance._Follower.WindowAnchor = TextAnchor.UpperCenter;
  101. //Module_Notice.getInstance.StartNotice(3f);
  102. this.SendCommand(new InstantiateCommand(InstantiateSystem.Instance.BlueObject.WarningPopUp));
  103. }
  104. }
  105. private void GetSNInit()
  106. {
  107. // 选择场景
  108. SendInit send = new SendInit();
  109. send.sn = SendSN.GetSN();
  110. //send.sn = API_GSXR_Slam.SlamManager.plugin.SN;
  111. send.projectId = m_CurrentData.id;
  112. string jsonString = JsonConvert.SerializeObject(send);
  113. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.sceneChoose);
  114. //通过SN获取场景数据
  115. HttpTool.Instance.PostTest("/sn/init", jsonString, (message) =>
  116. {
  117. if (message == "UnityWebRequest Error") return;
  118. Debug.Log("SN:" + send.sn);
  119. Debug.Log("通过SN获取场景数据:" + message);
  120. if (!string.IsNullOrWhiteSpace(message))
  121. {
  122. JObject jobject = JObject.Parse(message);
  123. if (jobject.GetValue("code") != null)
  124. {
  125. if (jobject["code"].ToString() == "400")
  126. {
  127. UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
  128. //Module_Notice.getInstance.SetNoticeInfo("场景数据获取失败", jobject["data"].ToString(), NoticeType.Normal, 1.7f);
  129. //Module_Notice.getInstance._Follower.WindowAnchor = TextAnchor.UpperCenter;
  130. //Module_Notice.getInstance.StartNotice(3f);
  131. }
  132. }
  133. else
  134. {
  135. message = jobject["data"].ToString();
  136. GameManager.Instance.text.text = message;
  137. // SceneValue scene = JsonMapper.ToObject<SceneValue>(message);
  138. SceneValue scene = JsonConvert.DeserializeObject<SceneValue>(message);
  139. DataManager.Instance.CurrentScene = scene;
  140. DataManager.Instance.ProjectID = m_CurrentData.id;
  141. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.createScene);
  142. StartCoroutine(GetSpoitValueDetail(scene));
  143. GameManager.Instance.text.text = JsonConvert.SerializeObject(scene);
  144. Debug.Log("当前选择场景数据: " + JsonConvert.SerializeObject(scene));
  145. }
  146. }
  147. else
  148. {
  149. UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
  150. }
  151. });
  152. }
  153. private IEnumerator GetSpoitValueDetail(SceneValue scene)
  154. {
  155. var spoits = scene.listSpoit;
  156. for (int i = 0; i < spoits.Count; i++)
  157. {
  158. //通过ID获取场景详情
  159. SendSceneDetail detail = new SendSceneDetail();
  160. detail.id = spoits[i].id;
  161. string jsonString = JsonConvert.SerializeObject(detail);
  162. yield return StartCoroutine(HttpTool.Instance.SendHttp("/viewpoint/detail", jsonString, (message) =>
  163. {
  164. //Debug.Log("当前选择场景数据: " + message);
  165. JObject jObject = JObject.Parse(message);
  166. if (jObject["code"].ToString() == "200")
  167. {
  168. message = jObject["data"].ToString();
  169. SpoitValueDetail spoit = JsonConvert.DeserializeObject<SpoitValueDetail>(message);
  170. GameManager.Instance.SpoitsValueDetail.Add(spoit.id, spoit);
  171. }
  172. }));
  173. }
  174. Debug.Log(scene.vuforiaXML);
  175. string xmlName = Path.GetFileName(scene.vuforiaXML);
  176. if (xmlName != null)
  177. {
  178. xmlName = xmlName.Substring(0, xmlName.Length - 4);
  179. AppConfigConst.IMAGE_TARGET_FILE_NAME = xmlName;
  180. }
  181. if (scene.vuforiaXML != null)
  182. {
  183. DownloadData xml = new DownloadData();
  184. string filename = Path.GetFileName(scene.vuforiaXML);
  185. xml.name = filename;
  186. xml.type = 8;
  187. xml.downloadPath = scene.vuforiaXML;
  188. xml.updateTime = (int)scene.updateTime;
  189. xml.localSavePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + filename;
  190. DownloadManager.Instance.AddDownloadData(xml);
  191. DownloadData dat = new DownloadData();
  192. string filename2 = Path.GetFileName(scene.vuforiaDat);
  193. dat.name = filename2;
  194. dat.type = 8;
  195. dat.downloadPath = scene.vuforiaDat;
  196. dat.updateTime = (int)scene.updateTime;
  197. dat.localSavePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + filename2;
  198. DownloadManager.Instance.AddDownloadData(dat);
  199. }
  200. yield return new WaitForSeconds(1f);
  201. //创建场景
  202. GameManager.Instance.CreateScene(scene);
  203. Hide();
  204. }
  205. private void UpdateSceneItem()
  206. {
  207. if (m_SceneData == null)
  208. {
  209. return;
  210. }
  211. ClearItem();
  212. for (int i = 0; i < m_SceneData.Count; i++)
  213. {
  214. var obj = Instantiate(m_SceneItem);
  215. obj.transform.SetParent(m_ScenePanel);
  216. var item = obj.AddComponent<SChooseItem>();
  217. obj.gameObject.SetActive(true);
  218. obj.transform.localScale = new Vector3(1, 1, 1);
  219. obj.transform.localEulerAngles = Vector3.zero;
  220. item.OnSelectScene += OnClickSceneItem;
  221. item.SceneData = m_SceneData[i];
  222. item.Init();
  223. m_Items.Add(item);
  224. }
  225. if (m_CarImage == null)
  226. {
  227. m_CarImage = m_ScenePanel.gameObject.AddComponent<CarouselImage>();
  228. }
  229. m_CarImage.mCellSize = new Vector2(400, 150);
  230. m_CarImage.mSpacing = new Vector2(0, 0);
  231. m_CarImage.mAutoLoop = false;
  232. m_CarImage.mDrag = true;
  233. m_CarImage.mLoopSpaceTime = 5f;
  234. m_CarImage.resizeChildren();
  235. }
  236. private void OnClickSceneItem(SceneValue item, bool b)
  237. {
  238. m_CurrentData = b ? item : null;
  239. }
  240. #endregion
  241. protected override void OnInit()
  242. {
  243. base.OnInit();
  244. }
  245. protected override void OnShow(object param)
  246. {
  247. base.OnShow(param);
  248. m_SceneData = (List<SceneValue>)param;
  249. UpdateSceneItem();
  250. }
  251. protected override void OnHide()
  252. {
  253. base.OnHide();
  254. }
  255. /// <summary>
  256. /// 清除所有场景信息
  257. /// </summary>
  258. private void ClearItem()
  259. {
  260. while (m_Items.Count > 0)
  261. {
  262. var item = m_Items[0];
  263. m_Items.RemoveAt(0);
  264. Destroy(item.gameObject);
  265. }
  266. }
  267. }