WindowsManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 = new Vector3(0, 0, 1);
  92. jt.transform.localEulerAngles = Vector3.zero;
  93. GameObject.Instantiate(GetPrefab(windowType.XunJian, "SaoMiao"), OpenXRCamera.Instance.head.transform);
  94. GameObject.Instantiate(GetPrefab(windowType.XunJian, "ARSaoTu"));
  95. GameObject.Instantiate(GetPrefab(windowType.ProjectMain, "PlayerToImage"));
  96. GameObject.Instantiate(GetPrefab(windowType.RTC, "GHZRtcNew"));
  97. GameObject.Instantiate(GetPrefab(windowType.RTC, "Tools"));
  98. tipInfoGameObject = GameObject.Instantiate(GetPrefab(windowType.ProjectMain, "TipInfo"));
  99. }
  100. public GameObject tipInfoGameObject;
  101. public static System.Type GetTypeByName(string name)
  102. {
  103. foreach (Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
  104. {
  105. foreach (System.Type type in assembly.GetTypes())
  106. {
  107. if (type.Name == name)
  108. return type;
  109. }
  110. }
  111. return null;
  112. }
  113. void initWindowList(List<windowItemGameObject> list, List<windowItemGameObject> nowlist,Transform parent)
  114. {
  115. if(list!=null)
  116. {
  117. for (int i = 0; i < list.Count; i++)
  118. {
  119. windowItemGameObject wg = new windowItemGameObject();
  120. wg.type = list[i].type;
  121. wg.parentType = list[i].parentType;
  122. wg.window = GameObject.Instantiate(list[i].window, parent);
  123. wg.window.name = list[i].window.name;
  124. wg.windowItemGameObjectList = new List<windowItemGameObject>();
  125. initGameObject(wg.window);
  126. initWindowList(list[i].windowItemGameObjectList, wg.windowItemGameObjectList, wg.window.transform);
  127. nowlist.Add(wg);
  128. }
  129. }
  130. }
  131. void initGameObject(GameObject go)
  132. {
  133. go.transform.localPosition = Vector3.zero;
  134. go.transform.localEulerAngles = Vector3.zero;
  135. go.transform.localScale = Vector3.one;
  136. }
  137. public GameObject GetPrefab(windowType wt,string name)
  138. {
  139. for (int i = 0; i < wConfig.list.Count; i++)
  140. {
  141. if(wConfig.list[i].type==wt)
  142. {
  143. for (int j = 0; j < wConfig.list[i].PrefabList.Count; j++)
  144. {
  145. if(wConfig.list[i].PrefabList[j].name==name)
  146. {
  147. return wConfig.list[i].PrefabList[j].obj;
  148. }
  149. }
  150. }
  151. }
  152. return null;
  153. }
  154. Queue<TipData> tdlist = new Queue<TipData>();
  155. void setWindowData(windowType wt,GameObject go, string data)
  156. {
  157. TipData td = new TipData();
  158. td.data = data;
  159. td.go = go;
  160. td.wt = wt;
  161. tdlist.Enqueue(td);
  162. }
  163. bool isChangzhu(windowType wt)
  164. {
  165. switch (wt)
  166. {
  167. case windowType.Top:
  168. return true;
  169. }
  170. return false;
  171. }
  172. bool isHuLue(windowType wt)
  173. {
  174. switch (wt)
  175. {
  176. case windowType.Tip:
  177. return true;
  178. case windowType.Tip2:
  179. return true;
  180. case windowType.Error:
  181. return true;
  182. }
  183. return false;
  184. }
  185. Stack<windowType> wlist = new Stack<windowType>();
  186. public void show(windowType wt,bool needHandle = true,string data="")
  187. {
  188. List<windowItemGameObject> wglist = getWindow(wt, needHandle);
  189. for (int i = 0; i < wglist.Count; i++)
  190. {
  191. bool isTip = isHuLue(wglist[i].type);
  192. Debug.Log("isTip===>"+ isTip);
  193. setWindowData(wglist[i].type, wglist[i].window, data);
  194. if(!isTip)
  195. showManagerWindow(windowItemGameObjectList, wglist, wglist[i].type, needHandle);
  196. }
  197. switch(wt)
  198. {
  199. case windowType.XunJianStart:
  200. closeWindow(windowItemGameObjectList,windowType.Top);
  201. break;
  202. case windowType.XunJianLB:
  203. showWindow(windowItemGameObjectList, windowType.Top);
  204. break;
  205. }
  206. if(wlist.Count>0)
  207. {
  208. if(wlist.Peek()!=wt&& !wlist.Contains(wt))
  209. {
  210. wlist.Push(wt);
  211. }
  212. }else
  213. {
  214. wlist.Push(wt);
  215. }
  216. }
  217. windowItemGameObject showitem(List<windowItemGameObject> list,windowType wt, bool needHandle = true)
  218. {
  219. for (int i = 0; i < list.Count; i++)
  220. {
  221. windowItemGameObject wg = showitem(list[i].windowItemGameObjectList, wt);
  222. if(wg!=null)
  223. {
  224. return wg;
  225. }
  226. if (list[i].type == wt)
  227. {
  228. return list[i];
  229. }
  230. }
  231. return null;
  232. }
  233. List<windowItemGameObject> getWindow(windowType wt, bool needHandle = true)
  234. {
  235. List<windowItemGameObject> wiList = new List<windowItemGameObject>();
  236. int ct = 0;
  237. while (true)
  238. {
  239. windowItemGameObject wg = checkWindow(wt, needHandle);
  240. if (wg==null)
  241. {
  242. return wiList;
  243. }else
  244. {
  245. wt = wg.parentType;
  246. wiList.Add(wg);
  247. }
  248. ct++;
  249. if(ct>10000)
  250. {
  251. Debug.LogError("窗口获取失败");
  252. return new List<windowItemGameObject>();
  253. }
  254. }
  255. }
  256. windowItemGameObject checkWindow( windowType wt, bool needHandle = true)
  257. {
  258. //寻找
  259. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  260. {
  261. if (windowItemGameObjectList[i].type == wt)
  262. {
  263. return windowItemGameObjectList[i];
  264. }
  265. windowItemGameObject wg = showitem(windowItemGameObjectList[i].windowItemGameObjectList, wt, needHandle);
  266. if (wg!=null)
  267. {
  268. return wg;
  269. }
  270. }
  271. return null;
  272. }
  273. void showWindow(List<windowItemGameObject> list, windowType wt)
  274. {
  275. //寻找
  276. for (int i = 0; i < list.Count; i++)
  277. {
  278. if (list[i].type == wt)
  279. {
  280. list[i].window.SetActive(true);
  281. }
  282. showWindow(list[i].windowItemGameObjectList, wt);
  283. }
  284. }
  285. void closeWindow(List<windowItemGameObject> list, windowType wt)
  286. {
  287. //寻找
  288. for (int i = 0; i < list.Count; i++)
  289. {
  290. if (list[i].type == wt)
  291. {
  292. list[i].window.SetActive(false);
  293. }
  294. closeWindow(list[i].windowItemGameObjectList, wt);
  295. }
  296. }
  297. void showManagerWindow(List<windowItemGameObject> list, List<windowItemGameObject> wglist, windowType wt, bool needHandle = true)
  298. {
  299. //寻找
  300. for (int i = 0; i < list.Count; i++)
  301. {
  302. if (list[i].type == wt)
  303. {
  304. list[i].window.SetActive(true) ;
  305. }
  306. else
  307. {
  308. if(!isChangzhu(list[i].type)&& needHandle&& !isHuLue(list[i].type))
  309. {
  310. bool isShow=false;
  311. for (int j = 0; j < wglist.Count; j++)
  312. {
  313. if(wglist[j].type== list[i].type)
  314. {
  315. isShow = true;
  316. }
  317. }
  318. if(!isShow)
  319. {
  320. list[i].window.SetActive(false);
  321. }
  322. }
  323. }
  324. showManagerWindow(list[i].windowItemGameObjectList, wglist, wt, needHandle);
  325. }
  326. }
  327. void updatePart()
  328. {
  329. if (tipInfoGameObject!=null&&API_GSXR_Module_InputSystem.GSXR_Position!=null)
  330. {
  331. tipInfoGameObject.transform.position = API_GSXR_Module_InputSystem.GSXR_Position;
  332. tipInfoGameObject.transform.LookAt(OpenXRCamera.Instance.head);
  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. saveNowWindowAndClose(false);
  368. ErrorManager em = td.go.GetComponent<ErrorManager>();
  369. td.go.SetActive(true);
  370. isShowTip = true;
  371. JsonData de = JsonMapper.ToObject(td.data);
  372. Color ce = new Color(float.Parse(de["cr"].ToString()), float.Parse(de["cg"].ToString()), float.Parse(de["cb"].ToString()), float.Parse(de["ca"].ToString()));
  373. List<string> bliste = new List<string>();
  374. bliste.Add(de["blist"][0].ToString());
  375. bliste.Add(de["blist"][1].ToString());
  376. bliste.Add(de["blist"][2].ToString());
  377. em.show(de["title"].ToString(), de["info"].ToString(), ce, getTexture(de["Texture"].ToString()), bliste, (string msg) => {
  378. saveNowWindowAndClose(true);
  379. OnTipBackChange?.Invoke(msg);
  380. },bool.Parse(de["isText"].ToString()), de["djsMsg"].ToString(),int.Parse(de["time"].ToString()), de["bt0name"].ToString(), de["bt1name"].ToString(), de["bt2name"].ToString());
  381. break;
  382. }
  383. }
  384. }
  385. List<windowItemGameObject> showList;
  386. void saveNowWindowAndClose(bool isRef)
  387. {
  388. if(!isRef)
  389. {
  390. showList = new List<windowItemGameObject>();
  391. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  392. {
  393. if (windowItemGameObjectList[i].window.activeSelf&& windowItemGameObjectList[i].type!= windowType.Error)
  394. {
  395. showList.Add(windowItemGameObjectList[i]);
  396. windowItemGameObjectList[i].window.SetActive(false);
  397. }
  398. }
  399. }else
  400. {
  401. for (int i = 0; i < showList.Count; i++)
  402. {
  403. showList[i].window.SetActive(true);
  404. }
  405. }
  406. }
  407. public Texture getTexture(string texId)
  408. {
  409. for (int i = 0; i < wConfig.listTexture.Count; i++)
  410. {
  411. if(wConfig.listTexture[i].PrefabList.name== texId)
  412. {
  413. return wConfig.listTexture[i].PrefabList.obj;
  414. }
  415. }
  416. return null;
  417. }
  418. 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 = "")
  419. {
  420. JsonData data = new JsonData();
  421. data["title"] = title;
  422. data["info"] = info;
  423. data["isText"] = isText;
  424. data["djsMsg"] = djsMsg;
  425. data["time"] = time;
  426. data["Texture"] = icon;
  427. data["bt0name"] = bt0;
  428. data["bt1name"] = bt1;
  429. data["bt2name"] = bt2;
  430. data["blist"] = new JsonData();
  431. for (int i = 0; i < 3; i++)
  432. {
  433. data["blist"].Add(backList[i]);
  434. }
  435. data["cr"] = color.r;
  436. data["cg"] = color.g;
  437. data["cb"] = color.b;
  438. data["ca"] = color.a;
  439. return data;
  440. }
  441. public JsonData getTip1Data(string title, string info, bool isQianWang, bool isSuccess, string texID, Color color, List<string> backList)
  442. {
  443. JsonData data = new JsonData();
  444. data["title"] = title;
  445. data["info"] = info;
  446. data["isQianWang"] = isQianWang;
  447. data["isSuccess"] = isSuccess;
  448. data["Texture"] = texID;
  449. data["blist"] = new JsonData();
  450. for (int i = 0; i < 3; i++)
  451. {
  452. data["blist"].Add(backList[i]);
  453. }
  454. data["cr"] = color.r;
  455. data["cg"] = color.g;
  456. data["cb"] = color.b;
  457. data["ca"] = color.a;
  458. return data;
  459. }
  460. void testTip()
  461. {
  462. WindowsManager.Instance.show(WindowConfig.windowType.Tip2, false, "敬请期待!");
  463. List<string> backTip = new List<string>();
  464. backTip.Add("test1");
  465. backTip.Add("test2");
  466. backTip.Add("test3");
  467. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("提示", "敬请期待!", Color.gray, "icon", backTip, true, "敬请期待", 5).ToJson());
  468. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("提示", "敬请期待!", Color.gray, "icon", backTip, false, "敬请期待", 5).ToJson());
  469. }
  470. public class TipData
  471. {
  472. public windowType wt;
  473. public GameObject go;
  474. public string data;
  475. }
  476. }