WindowsManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. PlayerPrefs.DeleteAll();
  17. StartCoroutine(CheckIsStart());
  18. }
  19. void initShowWindow()
  20. {
  21. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  22. {
  23. if(windowItemGameObjectList[i].type == windowsConfig.initShowWindow)
  24. {
  25. windowItemGameObjectList[i].window.SetActive(true);
  26. }
  27. else
  28. {
  29. windowItemGameObjectList[i].window.SetActive(false);
  30. }
  31. }
  32. }
  33. public bool isStart = false;
  34. IEnumerator CheckIsStart()
  35. {
  36. while (HttpSDKAction.Instance.jsonData == "")
  37. {
  38. yield return null;
  39. }
  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.window = GameObject.Instantiate(list[i].window, parent);
  71. wg.window.name = list[i].window.name;
  72. wg.windowItemGameObjectList = new List<windowItemGameObject>();
  73. initGameObject(wg.window);
  74. initWindowList(list[i].windowItemGameObjectList, wg.windowItemGameObjectList, wg.window.transform);
  75. nowlist.Add(wg);
  76. }
  77. }
  78. }
  79. void initGameObject(GameObject go)
  80. {
  81. go.transform.localPosition = Vector3.zero;
  82. go.transform.localEulerAngles = Vector3.zero;
  83. go.transform.localScale = Vector3.one;
  84. }
  85. public GameObject GetPrefab(windowType wt,string name)
  86. {
  87. for (int i = 0; i < wConfig.list.Count; i++)
  88. {
  89. if(wConfig.list[i].type==wt)
  90. {
  91. for (int j = 0; j < wConfig.list[i].PrefabList.Count; j++)
  92. {
  93. if(wConfig.list[i].PrefabList[j].name==name)
  94. {
  95. return wConfig.list[i].PrefabList[j].obj;
  96. }
  97. }
  98. }
  99. }
  100. return null;
  101. }
  102. void setWindowData(windowType wt,GameObject go, string data)
  103. {
  104. switch(wt)
  105. {
  106. case windowType.Tip2:
  107. Tip2Window t2w = go.GetComponent<Tip2Window>();
  108. t2w.showTxt(data);
  109. break;
  110. }
  111. }
  112. bool isChangzhu(windowType wt)
  113. {
  114. switch (wt)
  115. {
  116. case windowType.Top:
  117. return true;
  118. }
  119. return false;
  120. }
  121. bool isHuLue(windowType wt)
  122. {
  123. switch (wt)
  124. {
  125. case windowType.Tip:
  126. return true;
  127. case windowType.Tip2:
  128. return true;
  129. case windowType.Error:
  130. return true;
  131. }
  132. return false;
  133. }
  134. Stack<windowType> wlist = new Stack<windowType>();
  135. public void show(windowType wt,bool needHandle = true,string data="")
  136. {
  137. for (int i = 0; i < windowItemGameObjectList.Count; i++)
  138. {
  139. if (windowItemGameObjectList[i].type == wt)
  140. {
  141. windowItemGameObjectList[i].window.SetActive(true);
  142. setWindowData(wt, windowItemGameObjectList[i].window, data);
  143. }
  144. else
  145. {
  146. if(needHandle&&!isHuLue(windowItemGameObjectList[i].type))
  147. {
  148. windowItemGameObjectList[i].window.SetActive(isChangzhu(windowItemGameObjectList[i].type));
  149. }
  150. }
  151. if (showitem(windowItemGameObjectList[i].windowItemGameObjectList, wt, needHandle))
  152. {
  153. windowItemGameObjectList[i].window.SetActive(true);
  154. }
  155. }
  156. if(wlist.Count>0)
  157. {
  158. if(wlist.Peek()!=wt&& !wlist.Contains(wt))
  159. {
  160. wlist.Push(wt);
  161. }
  162. }else
  163. {
  164. wlist.Push(wt);
  165. }
  166. }
  167. bool showitem(List<windowItemGameObject> list,windowType wt, bool needHandle = true)
  168. {
  169. bool isShow=false;
  170. for (int i = 0; i < list.Count; i++)
  171. {
  172. showitem(list[i].windowItemGameObjectList, wt);
  173. if (list[i].type == wt)
  174. {
  175. list[i].window.SetActive(true);
  176. isShow = true;
  177. }
  178. else
  179. {
  180. if (needHandle && !isHuLue(list[i].type))
  181. {
  182. list[i].window.SetActive(isChangzhu(list[i].type));
  183. }
  184. }
  185. }
  186. return isShow;
  187. }
  188. }