WindowsManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEngine;
  7. using static WindowConfig;
  8. public class WindowsManager : MonoSingleton<WindowsManager>
  9. {
  10. public delegate void OnTipBackEvent(string msg);
  11. public OnTipBackEvent OnTipBackChange;
  12. public bool isShowTip;
  13. public WindowItemConfig wConfig;
  14. [HideInInspector]
  15. public Canvas canvasRoot;
  16. public List<windowItemGameObject> windowItemGameObjectList;
  17. public WindowConfig windowsConfig;
  18. private void Awake()
  19. {
  20. PlayerPrefs.DeleteAll();
  21. StartCoroutine(CheckIsStart());
  22. }
  23. void initShowWindow()
  24. {
  25. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  26. {
  27. if(windowItemGameObjectList[i].type == windowsConfig.initShowWindow)
  28. {
  29. windowItemGameObjectList[i].window.SetActive(true);
  30. }
  31. else
  32. {
  33. windowItemGameObjectList[i].window.SetActive(false);
  34. }
  35. }
  36. }
  37. public bool isStart = false;
  38. IEnumerator CheckIsStart()
  39. {
  40. while (HttpSDKAction.Instance.jsonData == "")
  41. {
  42. yield return null;
  43. }
  44. // RoadManager.Instance.gameObject.SetActive(false);
  45. isStart = true;
  46. canvasRoot = GameObject.Instantiate(windowsConfig.canvas,this.transform);
  47. for (int i = 0; i < windowsConfig.initComponent.Count; i++)
  48. {
  49. canvasRoot.gameObject.AddComponent(GetTypeByName(windowsConfig.initComponent[i].name));
  50. }
  51. windowItemGameObjectList = new List<windowItemGameObject>();
  52. initWindowList(windowsConfig.windowItemGameObjectList, windowItemGameObjectList, canvasRoot.transform);
  53. initShowWindow();
  54. Application.targetFrameRate = 300;
  55. GameObject jt = GameObject.Instantiate( GetPrefab(windowType.XunJian, "JianTou"),OpenXRCamera.Instance.head.transform);
  56. jt.transform.localPosition = new Vector3(0, 0, 1);
  57. jt.transform.localEulerAngles = Vector3.zero;
  58. GameObject.Instantiate(GetPrefab(windowType.XunJian, "SaoMiao"), OpenXRCamera.Instance.head.transform);
  59. GameObject.Instantiate(GetPrefab(windowType.XunJian, "ARSaoTu"));
  60. }
  61. public static System.Type GetTypeByName(string name)
  62. {
  63. foreach (Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
  64. {
  65. foreach (System.Type type in assembly.GetTypes())
  66. {
  67. if (type.Name == name)
  68. return type;
  69. }
  70. }
  71. return null;
  72. }
  73. void initWindowList(List<windowItemGameObject> list, List<windowItemGameObject> nowlist,Transform parent)
  74. {
  75. if(list!=null)
  76. {
  77. for (int i = 0; i < list.Count; i++)
  78. {
  79. windowItemGameObject wg = new windowItemGameObject();
  80. wg.type = list[i].type;
  81. wg.parentType = list[i].parentType;
  82. wg.window = GameObject.Instantiate(list[i].window, parent);
  83. wg.window.name = list[i].window.name;
  84. wg.windowItemGameObjectList = new List<windowItemGameObject>();
  85. initGameObject(wg.window);
  86. initWindowList(list[i].windowItemGameObjectList, wg.windowItemGameObjectList, wg.window.transform);
  87. nowlist.Add(wg);
  88. }
  89. }
  90. }
  91. void initGameObject(GameObject go)
  92. {
  93. go.transform.localPosition = Vector3.zero;
  94. go.transform.localEulerAngles = Vector3.zero;
  95. go.transform.localScale = Vector3.one;
  96. }
  97. public GameObject GetPrefab(windowType wt,string name)
  98. {
  99. for (int i = 0; i < wConfig.list.Count; i++)
  100. {
  101. if(wConfig.list[i].type==wt)
  102. {
  103. for (int j = 0; j < wConfig.list[i].PrefabList.Count; j++)
  104. {
  105. if(wConfig.list[i].PrefabList[j].name==name)
  106. {
  107. return wConfig.list[i].PrefabList[j].obj;
  108. }
  109. }
  110. }
  111. }
  112. return null;
  113. }
  114. Queue<TipData> tdlist = new Queue<TipData>();
  115. void setWindowData(windowType wt,GameObject go, string data)
  116. {
  117. TipData td = new TipData();
  118. td.data = data;
  119. td.go = go;
  120. td.wt = wt;
  121. tdlist.Enqueue(td);
  122. }
  123. bool isChangzhu(windowType wt)
  124. {
  125. switch (wt)
  126. {
  127. case windowType.Top:
  128. return true;
  129. }
  130. return false;
  131. }
  132. bool isHuLue(windowType wt)
  133. {
  134. switch (wt)
  135. {
  136. case windowType.Tip:
  137. return true;
  138. case windowType.Tip2:
  139. return true;
  140. case windowType.Error:
  141. return true;
  142. }
  143. return false;
  144. }
  145. Stack<windowType> wlist = new Stack<windowType>();
  146. public void show(windowType wt,bool needHandle = true,string data="")
  147. {
  148. List<windowItemGameObject> wglist = getWindow(wt, needHandle);
  149. for (int i = 0; i < wglist.Count; i++)
  150. {
  151. bool isTip = isHuLue(wglist[i].type);
  152. Debug.Log("isTip===>"+ isTip);
  153. setWindowData(wglist[i].type, wglist[i].window, data);
  154. if(!isTip)
  155. showManagerWindow(windowItemGameObjectList, wglist, wglist[i].type, needHandle);
  156. }
  157. switch(wt)
  158. {
  159. case windowType.XunJianStart:
  160. closeWindow(windowItemGameObjectList,windowType.Top);
  161. break;
  162. case windowType.XunJianLB:
  163. showWindow(windowItemGameObjectList, windowType.Top);
  164. break;
  165. }
  166. if(wlist.Count>0)
  167. {
  168. if(wlist.Peek()!=wt&& !wlist.Contains(wt))
  169. {
  170. wlist.Push(wt);
  171. }
  172. }else
  173. {
  174. wlist.Push(wt);
  175. }
  176. }
  177. windowItemGameObject showitem(List<windowItemGameObject> list,windowType wt, bool needHandle = true)
  178. {
  179. for (int i = 0; i < list.Count; i++)
  180. {
  181. windowItemGameObject wg = showitem(list[i].windowItemGameObjectList, wt);
  182. if(wg!=null)
  183. {
  184. return wg;
  185. }
  186. if (list[i].type == wt)
  187. {
  188. return list[i];
  189. }
  190. }
  191. return null;
  192. }
  193. List<windowItemGameObject> getWindow(windowType wt, bool needHandle = true)
  194. {
  195. List<windowItemGameObject> wiList = new List<windowItemGameObject>();
  196. int ct = 0;
  197. while (true)
  198. {
  199. windowItemGameObject wg = checkWindow(wt, needHandle);
  200. if (wg==null)
  201. {
  202. return wiList;
  203. }else
  204. {
  205. wt = wg.parentType;
  206. wiList.Add(wg);
  207. }
  208. ct++;
  209. if(ct>10000)
  210. {
  211. Debug.LogError("눗왯삿혤呵겨");
  212. return new List<windowItemGameObject>();
  213. }
  214. }
  215. }
  216. windowItemGameObject checkWindow( windowType wt, bool needHandle = true)
  217. {
  218. //璣冷
  219. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  220. {
  221. if (windowItemGameObjectList[i].type == wt)
  222. {
  223. return windowItemGameObjectList[i];
  224. }
  225. windowItemGameObject wg = showitem(windowItemGameObjectList[i].windowItemGameObjectList, wt, needHandle);
  226. if (wg!=null)
  227. {
  228. return wg;
  229. }
  230. }
  231. return null;
  232. }
  233. void showWindow(List<windowItemGameObject> list, windowType wt)
  234. {
  235. //璣冷
  236. for (int i = 0; i < list.Count; i++)
  237. {
  238. if (list[i].type == wt)
  239. {
  240. list[i].window.SetActive(true);
  241. }
  242. showWindow(list[i].windowItemGameObjectList, wt);
  243. }
  244. }
  245. void closeWindow(List<windowItemGameObject> list, windowType wt)
  246. {
  247. //璣冷
  248. for (int i = 0; i < list.Count; i++)
  249. {
  250. if (list[i].type == wt)
  251. {
  252. list[i].window.SetActive(false);
  253. }
  254. closeWindow(list[i].windowItemGameObjectList, wt);
  255. }
  256. }
  257. void showManagerWindow(List<windowItemGameObject> list, List<windowItemGameObject> wglist, windowType wt, bool needHandle = true)
  258. {
  259. //璣冷
  260. for (int i = 0; i < list.Count; i++)
  261. {
  262. if (list[i].type == wt)
  263. {
  264. list[i].window.SetActive(true) ;
  265. }
  266. else
  267. {
  268. if(!isChangzhu(list[i].type)&& needHandle&& !isHuLue(list[i].type))
  269. {
  270. bool isShow=false;
  271. for (int j = 0; j < wglist.Count; j++)
  272. {
  273. if(wglist[j].type== list[i].type)
  274. {
  275. isShow = true;
  276. }
  277. }
  278. if(!isShow)
  279. {
  280. list[i].window.SetActive(false);
  281. }
  282. }
  283. }
  284. showManagerWindow(list[i].windowItemGameObjectList, wglist, wt, needHandle);
  285. }
  286. }
  287. private void Update()
  288. {
  289. if(tdlist.Count>0&&!isShowTip)
  290. {
  291. TipData td = tdlist.Dequeue();
  292. Debug.Log("硫구鞫刻Tip");
  293. switch (td.wt)
  294. {
  295. case windowType.Tip2:
  296. Tip2Window t2w = td.go.GetComponent<Tip2Window>();
  297. td.go.SetActive(true);
  298. isShowTip = true;
  299. t2w.showTxt(td.data, () => {
  300. OnTipBackChange?.Invoke("");
  301. });
  302. break;
  303. case windowType.Tip:
  304. Tip1Window tw = td.go.GetComponent<Tip1Window>();
  305. td.go.SetActive(true);
  306. isShowTip = true;
  307. JsonData d = JsonMapper.ToObject(td.data);
  308. Color c = new Color(float.Parse(d["cr"].ToString()), float.Parse(d["cg"].ToString()), float.Parse(d["cb"].ToString()), float.Parse(d["ca"].ToString()));
  309. List<string> blist = new List<string>();
  310. blist.Add(d["blist"][0].ToString());
  311. blist.Add(d["blist"][1].ToString());
  312. blist.Add(d["blist"][2].ToString());
  313. 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)=> {
  314. OnTipBackChange?.Invoke(msg);
  315. });
  316. break;
  317. case windowType.Error:
  318. saveNowWindowAndClose(false);
  319. ErrorManager em = td.go.GetComponent<ErrorManager>();
  320. td.go.SetActive(true);
  321. isShowTip = true;
  322. JsonData de = JsonMapper.ToObject(td.data);
  323. Color ce = new Color(float.Parse(de["cr"].ToString()), float.Parse(de["cg"].ToString()), float.Parse(de["cb"].ToString()), float.Parse(de["ca"].ToString()));
  324. List<string> bliste = new List<string>();
  325. bliste.Add(de["blist"][0].ToString());
  326. bliste.Add(de["blist"][1].ToString());
  327. bliste.Add(de["blist"][2].ToString());
  328. em.show(de["title"].ToString(), de["info"].ToString(), ce, getTexture(de["Texture"].ToString()), bliste, (string msg) => {
  329. saveNowWindowAndClose(true);
  330. OnTipBackChange?.Invoke(msg);
  331. },bool.Parse(de["isText"].ToString()), de["djsMsg"].ToString(),int.Parse(de["time"].ToString()), de["bt0name"].ToString(), de["bt1name"].ToString(), de["bt2name"].ToString());
  332. break;
  333. }
  334. }
  335. }
  336. List<windowItemGameObject> showList;
  337. void saveNowWindowAndClose(bool isRef)
  338. {
  339. if(!isRef)
  340. {
  341. showList = new List<windowItemGameObject>();
  342. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  343. {
  344. if (windowItemGameObjectList[i].window.activeSelf&& windowItemGameObjectList[i].type!= windowType.Error)
  345. {
  346. showList.Add(windowItemGameObjectList[i]);
  347. windowItemGameObjectList[i].window.SetActive(false);
  348. }
  349. }
  350. }else
  351. {
  352. for (int i = 0; i < showList.Count; i++)
  353. {
  354. showList[i].window.SetActive(true);
  355. }
  356. }
  357. }
  358. public Texture getTexture(string texId)
  359. {
  360. for (int i = 0; i < wConfig.listTexture.Count; i++)
  361. {
  362. if(wConfig.listTexture[i].PrefabList.name== texId)
  363. {
  364. return wConfig.listTexture[i].PrefabList.obj;
  365. }
  366. }
  367. return null;
  368. }
  369. 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 = "")
  370. {
  371. JsonData data = new JsonData();
  372. data["title"] = title;
  373. data["info"] = info;
  374. data["isText"] = isText;
  375. data["djsMsg"] = djsMsg;
  376. data["time"] = time;
  377. data["Texture"] = icon;
  378. data["bt0name"] = bt0;
  379. data["bt1name"] = bt1;
  380. data["bt2name"] = bt2;
  381. data["blist"] = new JsonData();
  382. for (int i = 0; i < 3; i++)
  383. {
  384. data["blist"].Add(backList[i]);
  385. }
  386. data["cr"] = color.r;
  387. data["cg"] = color.g;
  388. data["cb"] = color.b;
  389. data["ca"] = color.a;
  390. return data;
  391. }
  392. public JsonData getTip1Data(string title, string info, bool isQianWang, bool isSuccess, string texID, Color color, List<string> backList)
  393. {
  394. JsonData data = new JsonData();
  395. data["title"] = title;
  396. data["info"] = info;
  397. data["isQianWang"] = isQianWang;
  398. data["isSuccess"] = isSuccess;
  399. data["Texture"] = texID;
  400. data["blist"] = new JsonData();
  401. for (int i = 0; i < 3; i++)
  402. {
  403. data["blist"].Add(backList[i]);
  404. }
  405. data["cr"] = color.r;
  406. data["cg"] = color.g;
  407. data["cb"] = color.b;
  408. data["ca"] = color.a;
  409. return data;
  410. }
  411. void testTip()
  412. {
  413. WindowsManager.Instance.show(WindowConfig.windowType.Tip2, false, "쓴헝퍅덤!");
  414. List<string> backTip = new List<string>();
  415. backTip.Add("test1");
  416. backTip.Add("test2");
  417. backTip.Add("test3");
  418. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("瓊刻", "쓴헝퍅덤!", Color.gray, "icon", backTip, true, "쓴헝퍅덤", 5).ToJson());
  419. WindowsManager.Instance.show(WindowConfig.windowType.Error, false, WindowsManager.Instance.getErrorData("瓊刻", "쓴헝퍅덤!", Color.gray, "icon", backTip, false, "쓴헝퍅덤", 5).ToJson());
  420. }
  421. public class TipData
  422. {
  423. public windowType wt;
  424. public GameObject go;
  425. public string data;
  426. }
  427. }