SceneChoose.cs 13 KB

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