WindowsManager.cs 14 KB

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