SceneChoose.cs 11 KB

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