WindowsManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using static WindowConfig;
  7. public class WindowsManager : MonoSingleton<WindowsManager>
  8. {
  9. public WindowItemConfig wConfig;
  10. [HideInInspector]
  11. public Canvas canvasRoot;
  12. public List<windowItemGameObject> windowItemGameObjectList;
  13. public WindowConfig windowsConfig;
  14. private void Awake()
  15. {
  16. StartCoroutine(CheckIsStart());
  17. }
  18. void initShowWindow()
  19. {
  20. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  21. {
  22. if(windowItemGameObjectList[i].type == windowsConfig.initShowWindow)
  23. {
  24. windowItemGameObjectList[i].window.SetActive(true);
  25. }
  26. else
  27. {
  28. windowItemGameObjectList[i].window.SetActive(false);
  29. }
  30. }
  31. }
  32. public bool isStart = false;
  33. IEnumerator CheckIsStart()
  34. {
  35. while (HttpSDKAction.Instance.jsonData == "")
  36. {
  37. yield return null;
  38. }
  39. // RoadManager.Instance.gameObject.SetActive(false);
  40. isStart = true;
  41. canvasRoot = GameObject.Instantiate(windowsConfig.canvas);
  42. for (int i = 0; i < windowsConfig.initComponent.Count; i++)
  43. {
  44. canvasRoot.gameObject.AddComponent(GetTypeByName(windowsConfig.initComponent[i].name));
  45. }
  46. windowItemGameObjectList = new List<windowItemGameObject>();
  47. initWindowList(windowsConfig.windowItemGameObjectList, windowItemGameObjectList, canvasRoot.transform);
  48. initShowWindow();
  49. }
  50. public static System.Type GetTypeByName(string name)
  51. {
  52. foreach (Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
  53. {
  54. foreach (System.Type type in assembly.GetTypes())
  55. {
  56. if (type.Name == name)
  57. return type;
  58. }
  59. }
  60. return null;
  61. }
  62. void initWindowList(List<windowItemGameObject> list, List<windowItemGameObject> nowlist,Transform parent)
  63. {
  64. if(list!=null)
  65. {
  66. for (int i = 0; i < list.Count; i++)
  67. {
  68. windowItemGameObject wg = new windowItemGameObject();
  69. wg.type = list[i].type;
  70. wg.parentType = list[i].parentType;
  71. wg.window = GameObject.Instantiate(list[i].window, parent);
  72. wg.window.name = list[i].window.name;
  73. wg.windowItemGameObjectList = new List<windowItemGameObject>();
  74. initGameObject(wg.window);
  75. initWindowList(list[i].windowItemGameObjectList, wg.windowItemGameObjectList, wg.window.transform);
  76. nowlist.Add(wg);
  77. }
  78. }
  79. }
  80. void initGameObject(GameObject go)
  81. {
  82. go.transform.localPosition = Vector3.zero;
  83. go.transform.localEulerAngles = Vector3.zero;
  84. go.transform.localScale = Vector3.one;
  85. }
  86. public GameObject GetPrefab(windowType wt,string name)
  87. {
  88. for (int i = 0; i < wConfig.list.Count; i++)
  89. {
  90. if(wConfig.list[i].type==wt)
  91. {
  92. for (int j = 0; j < wConfig.list[i].PrefabList.Count; j++)
  93. {
  94. if(wConfig.list[i].PrefabList[j].name==name)
  95. {
  96. return wConfig.list[i].PrefabList[j].obj;
  97. }
  98. }
  99. }
  100. }
  101. return null;
  102. }
  103. void setWindowData(windowType wt,GameObject go, string data)
  104. {
  105. switch(wt)
  106. {
  107. case windowType.Tip2:
  108. Tip2Window t2w = go.GetComponent<Tip2Window>();
  109. t2w.showTxt(data);
  110. break;
  111. }
  112. }
  113. bool isChangzhu(windowType wt)
  114. {
  115. switch (wt)
  116. {
  117. case windowType.Top:
  118. return true;
  119. }
  120. return false;
  121. }
  122. bool isHuLue(windowType wt)
  123. {
  124. switch (wt)
  125. {
  126. case windowType.Tip:
  127. return true;
  128. case windowType.Tip2:
  129. return true;
  130. case windowType.Error:
  131. return true;
  132. }
  133. return false;
  134. }
  135. Stack<windowType> wlist = new Stack<windowType>();
  136. public void show(windowType wt,bool needHandle = true,string data="")
  137. {
  138. List<windowItemGameObject> wglist = getWindow(wt, needHandle);
  139. for (int i = 0; i < wglist.Count; i++)
  140. {
  141. showWindow(windowItemGameObjectList, wglist, wglist[i].type, needHandle);
  142. setWindowData(wglist[i].type, wglist[i].window, data);
  143. }
  144. if(wlist.Count>0)
  145. {
  146. if(wlist.Peek()!=wt&& !wlist.Contains(wt))
  147. {
  148. wlist.Push(wt);
  149. }
  150. }else
  151. {
  152. wlist.Push(wt);
  153. }
  154. }
  155. windowItemGameObject showitem(List<windowItemGameObject> list,windowType wt, bool needHandle = true)
  156. {
  157. for (int i = 0; i < list.Count; i++)
  158. {
  159. windowItemGameObject wg = showitem(list[i].windowItemGameObjectList, wt);
  160. if(wg!=null)
  161. {
  162. return wg;
  163. }
  164. if (list[i].type == wt)
  165. {
  166. return list[i];
  167. }
  168. }
  169. return null;
  170. }
  171. List<windowItemGameObject> getWindow(windowType wt, bool needHandle = true)
  172. {
  173. List<windowItemGameObject> wiList = new List<windowItemGameObject>();
  174. int ct = 0;
  175. while (true)
  176. {
  177. windowItemGameObject wg = checkWindow(wt, needHandle);
  178. if (wg==null)
  179. {
  180. return wiList;
  181. }else
  182. {
  183. wt = wg.parentType;
  184. wiList.Add(wg);
  185. }
  186. ct++;
  187. if(ct>10000)
  188. {
  189. Debug.LogError("´°¿Ú»ñȡʧ°Ü");
  190. return new List<windowItemGameObject>();
  191. }
  192. }
  193. }
  194. windowItemGameObject checkWindow( windowType wt, bool needHandle = true)
  195. {
  196. //ѰÕÒ
  197. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  198. {
  199. if (windowItemGameObjectList[i].type == wt)
  200. {
  201. return windowItemGameObjectList[i];
  202. }
  203. windowItemGameObject wg = showitem(windowItemGameObjectList[i].windowItemGameObjectList, wt, needHandle);
  204. if (wg!=null)
  205. {
  206. return wg;
  207. }
  208. }
  209. return null;
  210. }
  211. void showWindow(List<windowItemGameObject> list, List<windowItemGameObject> wglist, windowType wt, bool needHandle = true)
  212. {
  213. //ѰÕÒ
  214. for (int i = 0; i < list.Count; i++)
  215. {
  216. if (list[i].type == wt)
  217. {
  218. list[i].window.SetActive(true) ;
  219. }
  220. else
  221. {
  222. if(!isChangzhu(list[i].type)&& needHandle&& !isHuLue(list[i].type))
  223. {
  224. bool isShow=false;
  225. for (int j = 0; j < wglist.Count; j++)
  226. {
  227. if(wglist[j].type== list[i].type)
  228. {
  229. isShow = true;
  230. }
  231. }
  232. if(!isShow)
  233. {
  234. list[i].window.SetActive(false);
  235. }
  236. }
  237. }
  238. showWindow(list[i].windowItemGameObjectList, wglist, wt, needHandle);
  239. }
  240. }
  241. }