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 bool isTest = true;
  16. public delegate void OnTipBackEvent(string msg);
  17. public OnTipBackEvent OnTipBackChange;
  18. public bool isShowTip;
  19. public WindowItemConfig wConfig;
  20. [HideInInspector]
  21. public Canvas canvasRoot;
  22. public List<windowItemGameObject> windowItemGameObjectList;
  23. public WindowConfig windowsConfig;
  24. public void DestroyText2D(Texture2D t2)
  25. {
  26. Destroy(t2);
  27. }
  28. InputDeviceHeadPart headPart;
  29. InputDeviceKSPart LeftPart;
  30. InputDeviceKSPart RightPart;
  31. private void Awake()
  32. {
  33. PlayerPrefs.DeleteAll();
  34. StartCoroutine(CheckIsStart());
  35. if (API_GSXR_Module_InputSystem_Head.GSXR_Head != null)
  36. {
  37. headPart = API_GSXR_Module_InputSystem_Head.GSXR_Head;
  38. //headPart.detectorBase.pointerBase.gameObject;
  39. }
  40. if (API_GSXR_Module_InputSystem_KS.GSXR_KSRight != null)
  41. {
  42. RightPart = API_GSXR_Module_InputSystem_KS.GSXR_KSRight;
  43. }
  44. if (API_GSXR_Module_InputSystem_KS.GSXR_KSLeft != null)
  45. {
  46. LeftPart = API_GSXR_Module_InputSystem_KS.GSXR_KSLeft;
  47. }
  48. }
  49. void initShowWindow()
  50. {
  51. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  52. {
  53. if(windowItemGameObjectList[i].type == windowsConfig.initShowWindow)
  54. {
  55. windowItemGameObjectList[i].window.SetActive(true);
  56. }
  57. else
  58. {
  59. windowItemGameObjectList[i].window.SetActive(false);
  60. }
  61. }
  62. }
  63. public bool isStart = false;
  64. IEnumerator CheckIsStart()
  65. {
  66. ///神奇代码 但是不能删除 删除子线程使用opencv会闪退
  67. try
  68. {
  69. Texture2D texture2D = Resources.Load("touxiang") as Texture2D;
  70. Mat imgMat = new Mat(texture2D.height, texture2D.width, CvType.CV_8UC4);
  71. Utils.texture2DToMat(texture2D, imgMat);
  72. // Destroy(texture2D);
  73. imgMat.release();
  74. }
  75. catch { }
  76. while (HttpSDKAction.Instance.jsonData == "")
  77. {
  78. yield return null;
  79. }
  80. // RoadManager.Instance.gameObject.SetActive(false);
  81. isStart = true;
  82. canvasRoot = GameObject.Instantiate(windowsConfig.canvas,this.transform);
  83. for (int i = 0; i < windowsConfig.initComponent.Count; i++)
  84. {
  85. canvasRoot.gameObject.AddComponent(GetTypeByName(windowsConfig.initComponent[i].name));
  86. }
  87. windowItemGameObjectList = new List<windowItemGameObject>();
  88. initWindowList(windowsConfig.windowItemGameObjectList, windowItemGameObjectList, canvasRoot.transform);
  89. initShowWindow();
  90. Application.targetFrameRate = 300;
  91. GameObject jt = GameObject.Instantiate( GetPrefab(windowType.XunJian, "JianTou"),OpenXRCamera.Instance.head.transform);
  92. jt.transform.localPosition = Vector3.forward;
  93. jt.transform.localEulerAngles = Vector3.zero;
  94. GameObject obj= GameObject.Instantiate(GetPrefab(windowType.XunJian, "SaoMiao"), OpenXRCamera.Instance.head.transform);
  95. obj.transform.localPosition = new Vector3(0,0,2f);
  96. obj.transform.localEulerAngles = Vector3.zero;
  97. GameObject.Instantiate(GetPrefab(windowType.XunJian, "ARSaoTu"));
  98. GameObject.Instantiate(GetPrefab(windowType.ProjectMain, "PlayerToImage"));
  99. GameObject.Instantiate(GetPrefab(windowType.RTC, "GHZRtcNew"));
  100. GameObject.Instantiate(GetPrefab(windowType.RTC, "Tools"));
  101. GameObject.Instantiate(GetPrefab(windowType.XunJian, "ImmersalSDK"));
  102. DianYunManager.Instance.stopSaoMiao();
  103. tipInfoGameObject = GameObject.Instantiate(GetPrefab(windowType.ProjectMain, "TipInfo"));
  104. }
  105. public GameObject tipInfoGameObject;
  106. public static System.Type GetTypeByName(string name)
  107. {
  108. foreach (Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
  109. {
  110. foreach (System.Type type in assembly.GetTypes())
  111. {
  112. if (type.Name == name)
  113. return type;
  114. }
  115. }
  116. return null;
  117. }
  118. void initWindowList(List<windowItemGameObject> list, List<windowItemGameObject> nowlist,Transform parent)
  119. {
  120. if(list!=null)
  121. {
  122. for (int i = 0; i < list.Count; i++)
  123. {
  124. windowItemGameObject wg = new windowItemGameObject();
  125. wg.type = list[i].type;
  126. wg.parentType = list[i].parentType;
  127. wg.window = GameObject.Instantiate(list[i].window, parent);
  128. wg.window.name = list[i].window.name;
  129. wg.windowItemGameObjectList = new List<windowItemGameObject>();
  130. initGameObject(wg.window);
  131. initWindowList(list[i].windowItemGameObjectList, wg.windowItemGameObjectList, wg.window.transform);
  132. nowlist.Add(wg);
  133. }
  134. }
  135. }
  136. void initGameObject(GameObject go)
  137. {
  138. go.transform.localPosition = Vector3.zero;
  139. go.transform.localEulerAngles = Vector3.zero;
  140. go.transform.localScale = Vector3.one;
  141. }
  142. public GameObject GetPrefab(windowType wt,string name)
  143. {
  144. for (int i = 0; i < wConfig.list.Count; i++)
  145. {
  146. if(wConfig.list[i].type==wt)
  147. {
  148. for (int j = 0; j < wConfig.list[i].PrefabList.Count; j++)
  149. {
  150. if(wConfig.list[i].PrefabList[j].name==name)
  151. {
  152. return wConfig.list[i].PrefabList[j].obj;
  153. }
  154. }
  155. }
  156. }
  157. return null;
  158. }
  159. Queue<TipData> tdlist = new Queue<TipData>();
  160. void setWindowData(windowType wt,GameObject go, string data)
  161. {
  162. TipData td = new TipData();
  163. td.data = data;
  164. td.go = go;
  165. td.wt = wt;
  166. tdlist.Enqueue(td);
  167. }
  168. bool isChangzhu(windowType wt)
  169. {
  170. switch (wt)
  171. {
  172. case windowType.Top:
  173. return true;
  174. }
  175. return false;
  176. }
  177. public float preJDInt;
  178. bool isHuLue(windowType wt)
  179. {
  180. switch (wt)
  181. {
  182. case windowType.Tip:
  183. return true;
  184. case windowType.Tip2:
  185. return true;
  186. case windowType.Error:
  187. return true;
  188. }
  189. return false;
  190. }
  191. Stack<windowType> wlist = new Stack<windowType>();
  192. public void show(windowType wt,bool needHandle = true,string data="")
  193. {
  194. List<windowItemGameObject> wglist = getWindow(wt, needHandle);
  195. for (int i = 0; i < wglist.Count; i++)
  196. {
  197. bool isTip = isHuLue(wglist[i].type);
  198. Debug.Log("isTip===>"+ isTip);
  199. setWindowData(wglist[i].type, wglist[i].window, data);
  200. if(!isTip)
  201. showManagerWindow(windowItemGameObjectList, wglist, wglist[i].type, needHandle);
  202. }
  203. switch(wt)
  204. {
  205. case windowType.XunJianStart:
  206. closeWindow(windowItemGameObjectList,windowType.Top);
  207. break;
  208. case windowType.XunJianLB:
  209. showWindow(windowItemGameObjectList, windowType.Top);
  210. break;
  211. }
  212. if(wlist.Count>0)
  213. {
  214. if(wlist.Peek()!=wt&& !wlist.Contains(wt))
  215. {
  216. wlist.Push(wt);
  217. }
  218. }else
  219. {
  220. wlist.Push(wt);
  221. }
  222. }
  223. windowItemGameObject showitem(List<windowItemGameObject> list,windowType wt, bool needHandle = true)
  224. {
  225. for (int i = 0; i < list.Count; i++)
  226. {
  227. windowItemGameObject wg = showitem(list[i].windowItemGameObjectList, wt);
  228. if(wg!=null)
  229. {
  230. return wg;
  231. }
  232. if (list[i].type == wt)
  233. {
  234. return list[i];
  235. }
  236. }
  237. return null;
  238. }
  239. List<windowItemGameObject> getWindow(windowType wt, bool needHandle = true)
  240. {
  241. List<windowItemGameObject> wiList = new List<windowItemGameObject>();
  242. int ct = 0;
  243. while (true)
  244. {
  245. windowItemGameObject wg = checkWindow(wt, needHandle);
  246. if (wg==null)
  247. {
  248. return wiList;
  249. }else
  250. {
  251. wt = wg.parentType;
  252. wiList.Add(wg);
  253. }
  254. ct++;
  255. if(ct>10000)
  256. {
  257. Debug.LogError("窗口获取失败");
  258. return new List<windowItemGameObject>();
  259. }
  260. }
  261. }
  262. windowItemGameObject checkWindow( windowType wt, bool needHandle = true)
  263. {
  264. //寻找
  265. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  266. {
  267. if (windowItemGameObjectList[i].type == wt)
  268. {
  269. return windowItemGameObjectList[i];
  270. }
  271. windowItemGameObject wg = showitem(windowItemGameObjectList[i].windowItemGameObjectList, wt, needHandle);
  272. if (wg!=null)
  273. {
  274. return wg;
  275. }
  276. }
  277. return null;
  278. }
  279. void showWindow(List<windowItemGameObject> list, windowType wt)
  280. {
  281. //寻找
  282. for (int i = 0; i < list.Count; i++)
  283. {
  284. if (list[i].type == wt)
  285. {
  286. list[i].window.SetActive(true);
  287. }
  288. showWindow(list[i].windowItemGameObjectList, wt);
  289. }
  290. }
  291. void closeWindow(List<windowItemGameObject> list, windowType wt)
  292. {
  293. //寻找
  294. for (int i = 0; i < list.Count; i++)
  295. {
  296. if (list[i].type == wt)
  297. {
  298. list[i].window.SetActive(false);
  299. }
  300. closeWindow(list[i].windowItemGameObjectList, wt);
  301. }
  302. }
  303. void showManagerWindow(List<windowItemGameObject> list, List<windowItemGameObject> wglist, windowType wt, bool needHandle = true)
  304. {
  305. //寻找
  306. for (int i = 0; i < list.Count; i++)
  307. {
  308. if (list[i].type == wt)
  309. {
  310. list[i].window.SetActive(true) ;
  311. }
  312. else
  313. {
  314. if(!isChangzhu(list[i].type)&& needHandle&& !isHuLue(list[i].type))
  315. {
  316. bool isShow=false;
  317. for (int j = 0; j < wglist.Count; j++)
  318. {
  319. if(wglist[j].type== list[i].type)
  320. {
  321. isShow = true;
  322. }
  323. }
  324. if(!isShow)
  325. {
  326. list[i].window.SetActive(false);
  327. }
  328. }
  329. }
  330. showManagerWindow(list[i].windowItemGameObjectList, wglist, wt, needHandle);
  331. }
  332. }
  333. void updatePart()
  334. {
  335. }
  336. private void Update()
  337. {
  338. // updatePart();
  339. if (tdlist.Count>0&&!isShowTip)
  340. {
  341. TipData td = tdlist.Dequeue();
  342. Debug.Log("准备显示Tip");
  343. switch (td.wt)
  344. {
  345. case windowType.Tip2:
  346. preJDInt = 30;
  347. Tip2Window t2w = td.go.GetComponent<Tip2Window>();
  348. td.go.SetActive(true);
  349. isShowTip = true;
  350. t2w.showTxt(td.data, () => {
  351. OnTipBackChange?.Invoke("");
  352. });
  353. break;
  354. case windowType.Tip:
  355. preJDInt = 30;
  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. }