WindowsManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. using LitJson;
  2. using OpenCVForUnity.CoreModule;
  3. using OpenCVForUnity.UnityUtils;
  4. using SC.XR.Unity.Module_InputSystem;
  5. using SC.XR.Unity.Module_InputSystem.InputDeviceGC.KS;
  6. using SC.XR.Unity.Module_InputSystem.InputDeviceHead;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Reflection;
  11. using UnityEngine;
  12. using static WindowConfig;
  13. public class WindowsManager : MonoSingleton<WindowsManager>
  14. {
  15. public delegate void OnTipBackEvent(string msg);
  16. public OnTipBackEvent OnTipBackChange;
  17. public bool isShowTip;
  18. public WindowItemConfig wConfig;
  19. [HideInInspector]
  20. public Canvas canvasRoot;
  21. public List<windowItemGameObject> windowItemGameObjectList;
  22. public WindowConfig windowsConfig;
  23. public void DestroyText2D(Texture2D t2)
  24. {
  25. Destroy(t2);
  26. }
  27. InputDeviceHeadPart headPart;
  28. InputDeviceKSPart LeftPart;
  29. InputDeviceKSPart RightPart;
  30. private void Awake()
  31. {
  32. // PlayerPrefs.DeleteAll();
  33. StartCoroutine(CheckIsStart());
  34. if (API_GSXR_Module_InputSystem_Head.GSXR_Head != null)
  35. {
  36. headPart = API_GSXR_Module_InputSystem_Head.GSXR_Head;
  37. //headPart.detectorBase.pointerBase.gameObject;
  38. }
  39. if (API_GSXR_Module_InputSystem_KS.GSXR_KSRight != null)
  40. {
  41. RightPart = API_GSXR_Module_InputSystem_KS.GSXR_KSRight;
  42. }
  43. if (API_GSXR_Module_InputSystem_KS.GSXR_KSLeft != null)
  44. {
  45. LeftPart = API_GSXR_Module_InputSystem_KS.GSXR_KSLeft;
  46. }
  47. }
  48. void initShowWindow()
  49. {
  50. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  51. {
  52. if(windowItemGameObjectList[i].type == windowsConfig.initShowWindow)
  53. {
  54. windowItemGameObjectList[i].window.SetActive(true);
  55. }
  56. else
  57. {
  58. windowItemGameObjectList[i].window.SetActive(false);
  59. }
  60. }
  61. }
  62. public bool isStart = false;
  63. IEnumerator CheckIsStart()
  64. {
  65. ///神奇代码 但是不能删除 删除子线程使用opencv会闪退
  66. try
  67. {
  68. Texture2D texture2D = Resources.Load("touxiang") as Texture2D;
  69. Mat imgMat = new Mat(texture2D.height, texture2D.width, CvType.CV_8UC4);
  70. Utils.texture2DToMat(texture2D, imgMat);
  71. // Destroy(texture2D);
  72. imgMat.release();
  73. }
  74. catch { }
  75. while (HttpSDKAction.Instance.jsonData == "")
  76. {
  77. yield return null;
  78. }
  79. // RoadManager.Instance.gameObject.SetActive(false);
  80. isStart = true;
  81. canvasRoot = GameObject.Instantiate(windowsConfig.canvas,this.transform);
  82. for (int i = 0; i < windowsConfig.initComponent.Count; i++)
  83. {
  84. canvasRoot.gameObject.AddComponent(GetTypeByName(windowsConfig.initComponent[i].name));
  85. }
  86. windowItemGameObjectList = new List<windowItemGameObject>();
  87. initWindowList(windowsConfig.windowItemGameObjectList, windowItemGameObjectList, canvasRoot.transform);
  88. initShowWindow();
  89. Application.targetFrameRate = 300;
  90. GameObject jt = GameObject.Instantiate( GetPrefab(windowType.XunJian, "JianTou"),OpenXRCamera.Instance.head.transform);
  91. jt.transform.localPosition = Vector3.zero;
  92. jt.transform.localEulerAngles = Vector3.zero;
  93. GameObject obj= GameObject.Instantiate(GetPrefab(windowType.XunJian, "SaoMiao"), OpenXRCamera.Instance.head.transform);
  94. obj.transform.localPosition = new Vector3(0,0,1.7f);
  95. obj.transform.localEulerAngles = Vector3.zero;
  96. GameObject.Instantiate(GetPrefab(windowType.XunJian, "ARSaoTu"));
  97. GameObject.Instantiate(GetPrefab(windowType.ProjectMain, "PlayerToImage"));
  98. GameObject.Instantiate(GetPrefab(windowType.RTC, "GHZRtcNew"));
  99. GameObject.Instantiate(GetPrefab(windowType.RTC, "Tools"));
  100. GameObject.Instantiate(GetPrefab(windowType.XunJian, "ImmersalSDK"));
  101. DianYunManager.Instance.stopSaoMiao();
  102. tipInfoGameObject = GameObject.Instantiate(GetPrefab(windowType.ProjectMain, "TipInfo"));
  103. }
  104. public GameObject tipInfoGameObject;
  105. public static System.Type GetTypeByName(string name)
  106. {
  107. foreach (Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
  108. {
  109. foreach (System.Type type in assembly.GetTypes())
  110. {
  111. if (type.Name == name)
  112. return type;
  113. }
  114. }
  115. return null;
  116. }
  117. void initWindowList(List<windowItemGameObject> list, List<windowItemGameObject> nowlist,Transform parent)
  118. {
  119. if(list!=null)
  120. {
  121. for (int i = 0; i < list.Count; i++)
  122. {
  123. windowItemGameObject wg = new windowItemGameObject();
  124. wg.type = list[i].type;
  125. wg.parentType = list[i].parentType;
  126. wg.window = GameObject.Instantiate(list[i].window, parent);
  127. wg.window.name = list[i].window.name;
  128. wg.windowItemGameObjectList = new List<windowItemGameObject>();
  129. initGameObject(wg.window);
  130. initWindowList(list[i].windowItemGameObjectList, wg.windowItemGameObjectList, wg.window.transform);
  131. nowlist.Add(wg);
  132. }
  133. }
  134. }
  135. void initGameObject(GameObject go)
  136. {
  137. go.transform.localPosition = Vector3.zero;
  138. go.transform.localEulerAngles = Vector3.zero;
  139. go.transform.localScale = Vector3.one;
  140. }
  141. public GameObject GetPrefab(windowType wt,string name)
  142. {
  143. for (int i = 0; i < wConfig.list.Count; i++)
  144. {
  145. if(wConfig.list[i].type==wt)
  146. {
  147. for (int j = 0; j < wConfig.list[i].PrefabList.Count; j++)
  148. {
  149. if(wConfig.list[i].PrefabList[j].name==name)
  150. {
  151. return wConfig.list[i].PrefabList[j].obj;
  152. }
  153. }
  154. }
  155. }
  156. return null;
  157. }
  158. Queue<TipData> tdlist = new Queue<TipData>();
  159. void setWindowData(windowType wt,GameObject go, string data)
  160. {
  161. TipData td = new TipData();
  162. td.data = data;
  163. td.go = go;
  164. td.wt = wt;
  165. tdlist.Enqueue(td);
  166. }
  167. bool isChangzhu(windowType wt)
  168. {
  169. switch (wt)
  170. {
  171. case windowType.Top:
  172. return true;
  173. }
  174. return false;
  175. }
  176. public float preJDInt;
  177. bool isHuLue(windowType wt)
  178. {
  179. switch (wt)
  180. {
  181. case windowType.Tip:
  182. return true;
  183. case windowType.Tip2:
  184. return true;
  185. case windowType.Error:
  186. return true;
  187. }
  188. return false;
  189. }
  190. Stack<windowType> wlist = new Stack<windowType>();
  191. public void show(windowType wt,bool needHandle = true,string data="")
  192. {
  193. List<windowItemGameObject> wglist = getWindow(wt, needHandle);
  194. for (int i = 0; i < wglist.Count; i++)
  195. {
  196. bool isTip = isHuLue(wglist[i].type);
  197. Debug.Log("isTip===>"+ isTip);
  198. setWindowData(wglist[i].type, wglist[i].window, data);
  199. if(!isTip)
  200. showManagerWindow(windowItemGameObjectList, wglist, wglist[i].type, needHandle);
  201. }
  202. switch(wt)
  203. {
  204. case windowType.XunJianStart:
  205. closeWindow(windowItemGameObjectList,windowType.Top);
  206. break;
  207. case windowType.XunJianLB:
  208. showWindow(windowItemGameObjectList, windowType.Top);
  209. break;
  210. }
  211. if(wlist.Count>0)
  212. {
  213. if(wlist.Peek()!=wt&& !wlist.Contains(wt))
  214. {
  215. wlist.Push(wt);
  216. }
  217. }else
  218. {
  219. wlist.Push(wt);
  220. }
  221. }
  222. windowItemGameObject showitem(List<windowItemGameObject> list,windowType wt, bool needHandle = true)
  223. {
  224. for (int i = 0; i < list.Count; i++)
  225. {
  226. windowItemGameObject wg = showitem(list[i].windowItemGameObjectList, wt);
  227. if(wg!=null)
  228. {
  229. return wg;
  230. }
  231. if (list[i].type == wt)
  232. {
  233. return list[i];
  234. }
  235. }
  236. return null;
  237. }
  238. List<windowItemGameObject> getWindow(windowType wt, bool needHandle = true)
  239. {
  240. List<windowItemGameObject> wiList = new List<windowItemGameObject>();
  241. int ct = 0;
  242. while (true)
  243. {
  244. windowItemGameObject wg = checkWindow(wt, needHandle);
  245. if (wg==null)
  246. {
  247. return wiList;
  248. }else
  249. {
  250. wt = wg.parentType;
  251. wiList.Add(wg);
  252. }
  253. ct++;
  254. if(ct>10000)
  255. {
  256. Debug.LogError("窗口获取失败");
  257. return new List<windowItemGameObject>();
  258. }
  259. }
  260. }
  261. windowItemGameObject checkWindow( windowType wt, bool needHandle = true)
  262. {
  263. //寻找
  264. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  265. {
  266. if (windowItemGameObjectList[i].type == wt)
  267. {
  268. return windowItemGameObjectList[i];
  269. }
  270. windowItemGameObject wg = showitem(windowItemGameObjectList[i].windowItemGameObjectList, wt, needHandle);
  271. if (wg!=null)
  272. {
  273. return wg;
  274. }
  275. }
  276. return null;
  277. }
  278. void showWindow(List<windowItemGameObject> list, windowType wt)
  279. {
  280. //寻找
  281. for (int i = 0; i < list.Count; i++)
  282. {
  283. if (list[i].type == wt)
  284. {
  285. list[i].window.SetActive(true);
  286. }
  287. showWindow(list[i].windowItemGameObjectList, wt);
  288. }
  289. }
  290. void closeWindow(List<windowItemGameObject> list, windowType wt)
  291. {
  292. //寻找
  293. for (int i = 0; i < list.Count; i++)
  294. {
  295. if (list[i].type == wt)
  296. {
  297. list[i].window.SetActive(false);
  298. }
  299. closeWindow(list[i].windowItemGameObjectList, wt);
  300. }
  301. }
  302. void showManagerWindow(List<windowItemGameObject> list, List<windowItemGameObject> wglist, windowType wt, bool needHandle = true)
  303. {
  304. //寻找
  305. for (int i = 0; i < list.Count; i++)
  306. {
  307. if (list[i].type == wt)
  308. {
  309. list[i].window.SetActive(true) ;
  310. }
  311. else
  312. {
  313. if(!isChangzhu(list[i].type)&& needHandle&& !isHuLue(list[i].type))
  314. {
  315. bool isShow=false;
  316. for (int j = 0; j < wglist.Count; j++)
  317. {
  318. if(wglist[j].type== list[i].type)
  319. {
  320. isShow = true;
  321. }
  322. }
  323. if(!isShow)
  324. {
  325. list[i].window.SetActive(false);
  326. }
  327. }
  328. }
  329. showManagerWindow(list[i].windowItemGameObjectList, wglist, wt, needHandle);
  330. }
  331. }
  332. void updatePart()
  333. {
  334. }
  335. private void Update()
  336. {
  337. // updatePart();
  338. if (tdlist.Count>0&&!isShowTip)
  339. {
  340. TipData td = tdlist.Dequeue();
  341. Debug.Log("准备显示Tip");
  342. switch (td.wt)
  343. {
  344. case windowType.Tip2:
  345. Tip2Window t2w = td.go.GetComponent<Tip2Window>();
  346. td.go.SetActive(true);
  347. isShowTip = true;
  348. t2w.showTxt(td.data, () => {
  349. OnTipBackChange?.Invoke("");
  350. });
  351. break;
  352. case windowType.Tip:
  353. Tip1Window tw = td.go.GetComponent<Tip1Window>();
  354. td.go.SetActive(true);
  355. isShowTip = true;
  356. JsonData d = JsonMapper.ToObject(td.data);
  357. Color c = new Color(float.Parse(d["cr"].ToString()), float.Parse(d["cg"].ToString()), float.Parse(d["cb"].ToString()), float.Parse(d["ca"].ToString()));
  358. List<string> blist = new List<string>();
  359. blist.Add(d["blist"][0].ToString());
  360. blist.Add(d["blist"][1].ToString());
  361. blist.Add(d["blist"][2].ToString());
  362. tw.show(d["title"].ToString(), d["info"].ToString(),bool.Parse( d["isQianWang"].ToString()), bool.Parse(d["isSuccess"].ToString()), getTexture(d["Texture"].ToString()), c, blist, (string msg)=> {
  363. OnTipBackChange?.Invoke(msg);
  364. });
  365. break;
  366. case windowType.Error:
  367. preJDInt = 30;
  368. if (WindowGenSui.Instance.jd >= 30)
  369. preJDInt = WindowGenSui.Instance.jd;
  370. WindowGenSui.Instance.jd = 10;
  371. saveNowWindowAndClose(false);
  372. ErrorManager em = td.go.GetComponent<ErrorManager>();
  373. td.go.SetActive(true);
  374. isShowTip = true;
  375. JsonData de = JsonMapper.ToObject(td.data);
  376. Color ce = new Color(float.Parse(de["cr"].ToString()), float.Parse(de["cg"].ToString()), float.Parse(de["cb"].ToString()), float.Parse(de["ca"].ToString()));
  377. List<string> bliste = new List<string>();
  378. bliste.Add(de["blist"][0].ToString());
  379. bliste.Add(de["blist"][1].ToString());
  380. bliste.Add(de["blist"][2].ToString());
  381. em.show(de["title"].ToString(), de["info"].ToString(), ce, getTexture(de["Texture"].ToString()), bliste, (string msg) => {
  382. saveNowWindowAndClose(true);
  383. OnTipBackChange?.Invoke(msg);
  384. },bool.Parse(de["isText"].ToString()), de["djsMsg"].ToString(),int.Parse(de["time"].ToString()), de["bt0name"].ToString(), de["bt1name"].ToString(), de["bt2name"].ToString());
  385. break;
  386. }
  387. }
  388. }
  389. List<windowItemGameObject> showList;
  390. void saveNowWindowAndClose(bool isRef)
  391. {
  392. if(!isRef)
  393. {
  394. showList = new List<windowItemGameObject>();
  395. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  396. {
  397. if (windowItemGameObjectList[i].window.activeSelf&& windowItemGameObjectList[i].type!= windowType.Error)
  398. {
  399. showList.Add(windowItemGameObjectList[i]);
  400. windowItemGameObjectList[i].window.SetActive(false);
  401. }
  402. }
  403. }else
  404. {
  405. for (int i = 0; i < showList.Count; i++)
  406. {
  407. showList[i].window.SetActive(true);
  408. }
  409. }
  410. }
  411. public Texture getTexture(string texId)
  412. {
  413. for (int i = 0; i < wConfig.listTexture.Count; i++)
  414. {
  415. if(wConfig.listTexture[i].PrefabList.name== texId)
  416. {
  417. return wConfig.listTexture[i].PrefabList.obj;
  418. }
  419. }
  420. return null;
  421. }
  422. public JsonData getErrorData(string title, string info, Color color, string icon, List<string> backList, bool isText = true, string djsMsg = "", int time = 5, string bt0 = "", string bt1 = "", string bt2 = "")
  423. {
  424. JsonData data = new JsonData();
  425. data["title"] = title;
  426. data["info"] = info;
  427. data["isText"] = isText;
  428. data["djsMsg"] = djsMsg;
  429. data["time"] = time;
  430. data["Texture"] = icon;
  431. data["bt0name"] = bt0;
  432. data["bt1name"] = bt1;
  433. data["bt2name"] = bt2;
  434. data["blist"] = new JsonData();
  435. for (int i = 0; i < 3; i++)
  436. {
  437. data["blist"].Add(backList[i]);
  438. }
  439. data["cr"] = color.r;
  440. data["cg"] = color.g;
  441. data["cb"] = color.b;
  442. data["ca"] = color.a;
  443. return data;
  444. }
  445. public JsonData getTip1Data(string title, string info, bool isQianWang, bool isSuccess, string texID, Color color, List<string> backList)
  446. {
  447. JsonData data = new JsonData();
  448. data["title"] = title;
  449. data["info"] = info;
  450. data["isQianWang"] = isQianWang;
  451. data["isSuccess"] = isSuccess;
  452. data["Texture"] = texID;
  453. data["blist"] = new JsonData();
  454. for (int i = 0; i < 3; i++)
  455. {
  456. data["blist"].Add(backList[i]);
  457. }
  458. data["cr"] = color.r;
  459. data["cg"] = color.g;
  460. data["cb"] = color.b;
  461. data["ca"] = color.a;
  462. return data;
  463. }
  464. void testTip()
  465. {
  466. WindowsManager.Instance.show(WindowConfig.windowType.Tip2, false, "敬请期待!");
  467. List<string> backTip = new List<string>();
  468. backTip.Add("test1");
  469. backTip.Add("test2");
  470. backTip.Add("test3");
  471. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("提示", "敬请期待!", Color.gray, "icon", backTip, true, "敬请期待", 5).ToJson());
  472. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("提示", "敬请期待!", Color.gray, "icon", backTip, false, "敬请期待", 5).ToJson());
  473. }
  474. public class TipData
  475. {
  476. public windowType wt;
  477. public GameObject go;
  478. public string data;
  479. }
  480. }