WindowsManager.cs 17 KB

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