WindowsManager.cs 17 KB

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