SceneChoose.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. using XRTool.Util;
  14. public class SceneChoose : BaseUI
  15. {
  16. public CustomHorizintalScrollView view;
  17. private Button m_LeftBtn;
  18. private Button m_RightBtn;
  19. private Button m_OkBtn;
  20. private GameObject m_SceneItem;
  21. private Transform m_ScenePanel;
  22. /// <summary>
  23. /// 场景选择面板中确认的场景
  24. /// </summary>
  25. private SceneValue m_CurrentData;
  26. private List<SChooseItem> m_Items;
  27. private CarouselImage m_CarImage;
  28. private List<SceneValue> m_SceneData;
  29. public static int Sid = -1;
  30. protected override void OnAwake()
  31. {
  32. base.OnAwake();
  33. m_SceneData = new List<SceneValue>();
  34. m_Items = new List<SChooseItem>();
  35. m_CurrentData = null;
  36. m_LeftBtn = CacheTransform.Find("Left").GetComponent<Button>();
  37. m_RightBtn = CacheTransform.Find("Right").GetComponent<Button>();
  38. m_OkBtn = CacheTransform.Find("OK").GetComponent<Button>();
  39. m_SceneItem = CacheTransform.Find("SceneChooseItem").gameObject;
  40. m_ScenePanel = CacheTransform.Find("ScenePanel");
  41. m_ScenePanel.GetComponent<RectTransform>().sizeDelta = new Vector2(400, 150);
  42. Debug.Log("CustomHorizintalScrollView " + view.name);
  43. //m_LeftBtn.onClick.AddListener(ONClickLeftBtn);
  44. //m_RightBtn.onClick.AddListener(ONClickRightBtn);
  45. //m_OkBtn.onClick.AddListener(OnClickOKBtn);
  46. }
  47. #region UI监听事件
  48. private void ONClickLeftBtn()
  49. {
  50. if (m_CarImage != null)
  51. {
  52. m_CarImage.MoveToIndex(m_CarImage.CurrentIndex + 1);
  53. }
  54. }
  55. private void ONClickRightBtn()
  56. {
  57. if (m_CarImage != null)
  58. {
  59. m_CarImage.MoveToIndex(m_CarImage.CurrentIndex - 1);
  60. }
  61. }
  62. public void OnClickOKBtn(SceneValue m_CurrentData)
  63. {
  64. // GetComponent<RectTransform>().position = new Vector3(1000000,42.1f,0);
  65. this.m_CurrentData = m_CurrentData;
  66. MQTTClient.Instance.SubscribeScene(this.m_CurrentData.id.ToString());
  67. Sid = this.m_CurrentData.id;
  68. //根据场景数据,下载资源
  69. Debug.Log("选择的场景为:" + m_CurrentData.name);
  70. SendSceneDetail detail = new SendSceneDetail();
  71. detail.id = m_CurrentData.id;
  72. string str = JsonConvert.SerializeObject(detail);
  73. this.GetModel<ISceneModel>().sceneID = m_CurrentData.id;
  74. this.SendCommand(new PointFileGetUrlCommand(detail.id, PointFileType.json));
  75. this.SendCommand(new PointFileGetUrlCommand(detail.id, PointFileType.bytes));
  76. this.SendCommand(new PointPosRotDownloadCommand(detail.id));
  77. this.SendCommand(new MinMapGetSetCommand());
  78. HttpTool.Instance.PostTest("/project/detail", str, (mes) =>
  79. {
  80. if (mes == "UnityWebRequest Error") return;
  81. JObject jObject = JObject.Parse(mes);
  82. if (jObject["code"].ToString() == "200")
  83. {
  84. mes = jObject["data"].ToString();
  85. SceneValue scene = JsonConvert.DeserializeObject<SceneValue>(mes);
  86. Debug.Log("scene.backgroundImages===>" + mes);
  87. Debug.Log("scene.backgroundImages===>"+ scene.backgroundImages.Count);
  88. GameManager.Instance.MapPicUrl = scene.backgroundImages[0].base64;
  89. GameManager.Instance.GetMinMap(scene, (bool back) =>
  90. {
  91. Debug.Log("scene.backgroundImages===>back"+ back);
  92. if (back)
  93. {
  94. GetSNInit();
  95. this.SendCommand(new GetReferencePosCommand(detail.id));
  96. }
  97. else
  98. {
  99. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), scene+" 通讯出错 请尝试重启应用");
  100. m_OkBtn.interactable = true;
  101. }
  102. } );
  103. }
  104. else
  105. {
  106. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "/project/detail 通讯出错 请尝试重启应用");
  107. m_OkBtn.interactable = true;
  108. }
  109. });
  110. /*
  111. if (m_CurrentData != null)
  112. {
  113. }
  114. else
  115. {
  116. ///Module_Notice.getInstance.SetNoticeInfo("未选择导览场景", "请先勾选要进入的场景", NoticeType.Normal, 1.7f);
  117. //Module_Notice.getInstance._Follower.WindowAnchor = TextAnchor.UpperCenter;
  118. //Module_Notice.getInstance.StartNotice(3f);
  119. // this.SendCommand(new InstantiateCommand(InstantiateSystem.Instance.BlueObject.WarningPopUp));
  120. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "请选择场景");
  121. TimerMgr.Instance.CreateTimer(()=> {
  122. UIManager.Instance.HideUI(UINameConfig.LoadingPanel);
  123. m_OkBtn.interactable = true;
  124. },1f);
  125. }
  126. */
  127. }
  128. private IEnumerator LoadCRT()
  129. {
  130. #if UNITY_ANDROID
  131. string streamingAssetsPath = "jar:file://" + Application.dataPath + "!/assets/";
  132. string fileName = "";
  133. string filePath = Path.Combine(streamingAssetsPath, fileName);
  134. Debug.Log("DGJ ===> filePath " + filePath);
  135. using (WWW www = new WWW(filePath))
  136. {
  137. yield return www;
  138. byte[] bytes = www.bytes;
  139. // MQTTClient.Instance.bytes = bytes;
  140. Debug.Log("DGJ ===> bytes " + bytes.Length);
  141. }
  142. #endif
  143. }
  144. private void GetSNInit()
  145. {
  146. // 选择场景
  147. SendInit send = new SendInit();
  148. send.sn = SendSN.GetSN();
  149. //send.sn = API_GSXR_Slam.SlamManager.plugin.SN;
  150. send.projectId = m_CurrentData.id;
  151. string jsonString = JsonConvert.SerializeObject(send);
  152. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.sceneChoose);
  153. //通过SN获取场景数据
  154. HttpTool.Instance.PostTest("/sn/init", jsonString, (message) =>
  155. {
  156. if (message == "UnityWebRequest Error") return;
  157. Debug.Log("SN:" + send.sn);
  158. Debug.Log("通过SN获取场景数据:" + message);
  159. if (!string.IsNullOrWhiteSpace(message))
  160. {
  161. Debug.Log("Hjj 当前选择场景数据 1: " + message);
  162. JsonData jobject = JsonMapper.ToObject(message);
  163. if (!jobject.Keys.Contains("code") ||jobject["code"].ToJson() == "400")
  164. {
  165. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "/sn/init 通讯出错 请尝试重启应用");
  166. }
  167. else
  168. {
  169. message = jobject["data"]["info"].ToJson();
  170. SceneValue scene = JsonConvert.DeserializeObject<SceneValue>(message);
  171. DataManager.Instance.CurrentScene = scene;
  172. DataManager.Instance.ProjectID = m_CurrentData.id;
  173. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), (int)ELoadState.createScene);
  174. StartCoroutine(GetSpoitValueDetail(scene));
  175. }
  176. }
  177. else
  178. {
  179. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "/sn/init 通讯出错 请尝试重启应用");
  180. }
  181. });
  182. }
  183. private IEnumerator GetSpoitValueDetail(SceneValue scene)
  184. {
  185. var spoits = scene.listSpoit;
  186. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "正在获取服务器景点信息");
  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.SendHttp("/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. Debug.Log("Hjj jObjectjObjectjObject " + message);
  201. SpoitValueDetail spoit = JsonConvert.DeserializeObject<SpoitValueDetail>(message);
  202. if(!GameManager.Instance.SpoitsValueDetail.ContainsKey(spoit.id))
  203. GameManager.Instance.SpoitsValueDetail.Add(spoit.id, spoit);
  204. else
  205. {
  206. GameManager.Instance.SpoitsValueDetail[spoit.id] = spoit;
  207. }
  208. }else
  209. {
  210. UIManager.Instance.ShowUI(UINameConfig.LoadingPanel, typeof(LoadingPanel), "/viewpoint/detail 通讯出错 请尝试重启应用");
  211. }
  212. }));
  213. }
  214. Debug.Log("Hjj scene.vuforiaXML" + scene.vuforiaXML);
  215. if(UserInfo.Instance.is20)
  216. {
  217. string xmlName = Path.GetFileName(scene.vuforiaXML);
  218. if (xmlName != null)
  219. {
  220. xmlName = xmlName.Substring(0, xmlName.Length - 4);
  221. AppConfigConst.IMAGE_TARGET_FILE_NAME = xmlName;
  222. }
  223. if (scene.vuforiaXML != null)
  224. {
  225. if(Directory.Exists(Application.persistentDataPath + "/StreamingAssets"))
  226. Directory.Delete(Application.persistentDataPath + "/StreamingAssets");
  227. //DownloadData xml = new DownloadData();
  228. //string filename = Path.GetFileName(scene.vuforiaXML);
  229. //xml.name = filename;
  230. //xml.type = 8;
  231. //xml.downloadPath = scene.vuforiaXML;
  232. //xml.updateTime = (int)scene.updateTime;
  233. //xml.localSavePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + filename;
  234. //DownloadManager.Instance.AddDownloadData(xml);
  235. //DownloadData dat = new DownloadData();
  236. //string filename2 = Path.GetFileName(scene.vuforiaDat);
  237. //dat.name = filename2;
  238. //dat.type = 8;
  239. //dat.downloadPath = scene.vuforiaDat;
  240. //dat.updateTime = (int)scene.updateTime;
  241. //dat.localSavePath = Application.persistentDataPath + "/StreamingAssets/Vuforia/" + filename2;
  242. //DownloadManager.Instance.AddDownloadData(dat);
  243. DownLoadMaterial xml = new DownLoadMaterial();
  244. string filename = Path.GetFileName(scene.vuforiaXML);
  245. // xml.name = filename;
  246. xml.type = "-2";
  247. xml.downLoadPath = scene.vuforiaXML;
  248. xml.updataTime = scene.updateTime;
  249. xml.localLoadPath = Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia." + filename.Split('.')[1];
  250. // DownloadManager.Instance.AddDownloadData(xml);
  251. DownloadResManager.Instance.DownLoad(xml);
  252. DownLoadMaterial dat = new DownLoadMaterial();
  253. string filename2 = Path.GetFileName(scene.vuforiaDat);
  254. //dat.name = filename2;
  255. dat.type = "-2";
  256. dat.downLoadPath = scene.vuforiaDat;
  257. dat.updataTime = (int)scene.updateTime;
  258. dat.localLoadPath = Application.persistentDataPath + "/StreamingAssets/Vuforia/GHZVuforia." + filename2.Split('.')[1];
  259. DownloadResManager.Instance.DownLoad(dat);
  260. }
  261. }
  262. yield return new WaitForSeconds(1f);
  263. while(GameManager.Instance.isShowDownTip)
  264. {
  265. yield return null;
  266. }
  267. Debug.Log("创建场景123");
  268. //创建场景
  269. StartCoroutine(GameManager.Instance.CreateScene(scene));
  270. Hide();
  271. }
  272. public RectTransform scrollview;
  273. private void UpdateSceneItem()
  274. {
  275. Debug.Log("UpdateSceneItem1");
  276. if (m_SceneData == null)
  277. {
  278. return;
  279. }
  280. ClearItem();
  281. Debug.Log("UpdateSceneItem2");
  282. for (int i = 0; i < m_SceneData.Count; i++)
  283. {
  284. Debug.Log("UpdateSceneItem21");
  285. var obj = Instantiate(m_SceneItem);
  286. Debug.Log("UpdateSceneItem22");
  287. obj.transform.SetParent(m_ScenePanel);
  288. var item = obj.GetComponent<SChooseItem>();
  289. obj.gameObject.SetActive(true);
  290. obj.transform.localScale = new Vector3(1, 1, 1);
  291. obj.transform.localEulerAngles = Vector3.zero;
  292. item.OnSelectScene += OnClickSceneItem;
  293. item.SceneData = m_SceneData[i];
  294. m_Items.Add(item);
  295. }
  296. switch(m_SceneData.Count)
  297. {
  298. case 1:
  299. scrollview.sizeDelta = new Vector2(200,266);
  300. break;
  301. case 2:
  302. scrollview.sizeDelta = new Vector2(500, 266);
  303. break;
  304. }
  305. Debug.Log("UpdateSceneItem3");
  306. if (m_CarImage == null)
  307. {
  308. m_CarImage = m_ScenePanel.gameObject.AddComponent<CarouselImage>();
  309. }
  310. Debug.Log("UpdateSceneItem4");
  311. Debug.Log("CustomHorizintalScrollView " + view.name);
  312. view.Init(m_SceneData,this);
  313. Debug.Log("UpdateSceneItem6");
  314. m_CarImage.mCellSize = new Vector2(400, 150);
  315. m_CarImage.mSpacing = new Vector2(0, 0);
  316. m_CarImage.mAutoLoop = false;
  317. m_CarImage.mDrag = true;
  318. m_CarImage.mLoopSpaceTime = 5f;
  319. m_CarImage.resizeChildren();
  320. Debug.Log("UpdateSceneItem5");
  321. }
  322. private void OnClickSceneItem(SceneValue item, bool b)
  323. {
  324. m_CurrentData = b ? item : null;
  325. }
  326. #endregion
  327. protected override void OnInit()
  328. {
  329. base.OnInit();
  330. }
  331. protected override void OnShow(object param)
  332. {
  333. base.OnShow(param);
  334. m_SceneData = (List<SceneValue>)param;
  335. UpdateSceneItem();
  336. }
  337. protected override void OnHide()
  338. {
  339. base.OnHide();
  340. }
  341. /// <summary>
  342. /// 清除所有场景信息
  343. /// </summary>
  344. private void ClearItem()
  345. {
  346. while (m_Items.Count > 0)
  347. {
  348. var item = m_Items[0];
  349. m_Items.RemoveAt(0);
  350. Destroy(item.gameObject);
  351. }
  352. }
  353. }