WindowsManager.cs 14 KB

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