WindowsManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. isStart = true;
  40. canvasRoot = GameObject.Instantiate(windowsConfig.canvas);
  41. for (int i = 0; i < windowsConfig.initComponent.Count; i++)
  42. {
  43. canvasRoot.gameObject.AddComponent(GetTypeByName(windowsConfig.initComponent[i].name));
  44. }
  45. windowItemGameObjectList = new List<windowItemGameObject>();
  46. initWindowList(windowsConfig.windowItemGameObjectList, windowItemGameObjectList, canvasRoot.transform);
  47. initShowWindow();
  48. }
  49. public static System.Type GetTypeByName(string name)
  50. {
  51. foreach (Assembly assembly in System.AppDomain.CurrentDomain.GetAssemblies())
  52. {
  53. foreach (System.Type type in assembly.GetTypes())
  54. {
  55. if (type.Name == name)
  56. return type;
  57. }
  58. }
  59. return null;
  60. }
  61. void initWindowList(List<windowItemGameObject> list, List<windowItemGameObject> nowlist,Transform parent)
  62. {
  63. if(list!=null)
  64. {
  65. for (int i = 0; i < list.Count; i++)
  66. {
  67. windowItemGameObject wg = new windowItemGameObject();
  68. wg.type = list[i].type;
  69. wg.window = GameObject.Instantiate(list[i].window, parent);
  70. wg.window.name = list[i].window.name;
  71. wg.windowItemGameObjectList = new List<windowItemGameObject>();
  72. initGameObject(wg.window);
  73. initWindowList(list[i].windowItemGameObjectList, wg.windowItemGameObjectList, wg.window.transform);
  74. nowlist.Add(wg);
  75. }
  76. }
  77. }
  78. void initGameObject(GameObject go)
  79. {
  80. go.transform.localPosition = Vector3.zero;
  81. go.transform.localEulerAngles = Vector3.zero;
  82. go.transform.localScale = Vector3.one;
  83. }
  84. public GameObject GetPrefab(windowType wt,string name)
  85. {
  86. for (int i = 0; i < wConfig.list.Count; i++)
  87. {
  88. if(wConfig.list[i].type==wt)
  89. {
  90. for (int j = 0; j < wConfig.list[i].PrefabList.Count; j++)
  91. {
  92. if(wConfig.list[i].PrefabList[j].name==name)
  93. {
  94. return wConfig.list[i].PrefabList[j].obj;
  95. }
  96. }
  97. }
  98. }
  99. return null;
  100. }
  101. void setWindowData(windowType wt,GameObject go, string data)
  102. {
  103. switch(wt)
  104. {
  105. case windowType.Tip2:
  106. Tip2Window t2w = go.GetComponent<Tip2Window>();
  107. t2w.showTxt(data);
  108. break;
  109. }
  110. }
  111. bool isChangzhu(windowType wt)
  112. {
  113. switch (wt)
  114. {
  115. case windowType.Top:
  116. return true;
  117. }
  118. return false;
  119. }
  120. bool isHuLue(windowType wt)
  121. {
  122. switch (wt)
  123. {
  124. case windowType.Tip:
  125. return true;
  126. case windowType.Tip2:
  127. return true;
  128. case windowType.Error:
  129. return true;
  130. }
  131. return false;
  132. }
  133. Stack<windowType> wlist = new Stack<windowType>();
  134. public void show(windowType wt,bool needHandle = true,string data="")
  135. {
  136. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  137. {
  138. if (windowItemGameObjectList[i].type == wt)
  139. {
  140. windowItemGameObjectList[i].window.SetActive(true);
  141. setWindowData(wt, windowItemGameObjectList[i].window, data);
  142. }
  143. else
  144. {
  145. if(needHandle&&!isHuLue(windowItemGameObjectList[i].type))
  146. {
  147. windowItemGameObjectList[i].window.SetActive(isChangzhu(windowItemGameObjectList[i].type));
  148. }
  149. }
  150. if (showitem(windowItemGameObjectList[i].windowItemGameObjectList, wt, needHandle))
  151. {
  152. windowItemGameObjectList[i].window.SetActive(true);
  153. }
  154. }
  155. if(wlist.Count>0)
  156. {
  157. if(wlist.Peek()!=wt&& !wlist.Contains(wt))
  158. {
  159. wlist.Push(wt);
  160. }
  161. }else
  162. {
  163. wlist.Push(wt);
  164. }
  165. }
  166. bool showitem(List<windowItemGameObject> list,windowType wt, bool needHandle = true)
  167. {
  168. bool isShow=false;
  169. for (int i = 0; i < list.Count; i++)
  170. {
  171. showitem(list[i].windowItemGameObjectList, wt);
  172. if (list[i].type == wt)
  173. {
  174. list[i].window.SetActive(true);
  175. isShow = true;
  176. }
  177. else
  178. {
  179. if (needHandle && !isHuLue(list[i].type))
  180. {
  181. list[i].window.SetActive(isChangzhu(list[i].type));
  182. }
  183. }
  184. }
  185. return isShow;
  186. }
  187. }