ViewManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. using FairyGUI;
  5. using System.Linq;
  6. using ET;
  7. namespace GFGGame
  8. {
  9. /// <summary>
  10. /// ��ͼ������
  11. /// ������ͼ����ʾ������
  12. /// </summary>
  13. public class ViewManager
  14. {
  15. private static Dictionary<string, UIView> _viewDic;
  16. private static GComponent _bottomLayer;
  17. private static GComponent _topLayer;
  18. private static GComponent _guideLayer;
  19. private static GComponent _modalLayer;
  20. private static GComponent _alertLayer;
  21. private static GComponent _debugLayer;
  22. private static GComponent _floatLayer;
  23. private static Dictionary<string, object[]> _goBackDatas = new Dictionary<string, object[]>();
  24. public static void Init()
  25. {
  26. GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("Common"));
  27. UIConfig.buttonSound = (NAudioClip)UIPackage.GetItemAsset("Common", "click");
  28. Font afont = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("STZHONGS"));
  29. FontManager.RegisterFont(new DynamicFont("STZhongsong", afont));
  30. UIConfig.defaultFont = "STZhongsong";
  31. Font afont1 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("ZIHUN125"));
  32. FontManager.RegisterFont(new DynamicFont("ZIHUN125", afont1));
  33. Font afont2 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SIMKAI"));
  34. FontManager.RegisterFont(new DynamicFont("SIMKAI", afont2));
  35. Font afont3 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SHUANGYUJUTI"));
  36. FontManager.RegisterFont(new DynamicFont("SHUANGYUJUTI", afont3));
  37. _viewDic = new Dictionary<string, UIView>();
  38. //��ʼ����ͼ������
  39. _bottomLayer = CreateLayer("BottomLayer");
  40. _topLayer = CreateLayer("TopLayer");
  41. _topLayer.sortingOrder = ConstSortingOrder.TOP;
  42. _guideLayer = CreateLayer("GuideLayer");
  43. _guideLayer.sortingOrder = ConstSortingOrder.Guide;
  44. _modalLayer = CreateLayer("ModalLayer");
  45. _modalLayer.sortingOrder = ConstSortingOrder.Modal;
  46. _alertLayer = CreateLayer("AlertLayer");
  47. _alertLayer.sortingOrder = ConstSortingOrder.Alert;
  48. //debug层
  49. _debugLayer = CreateLayer("DebugLayer");
  50. _debugLayer.sortingOrder = ConstSortingOrder.Debug;
  51. _floatLayer = CreateLayer("FloatLayer");
  52. _floatLayer.sortingOrder = ConstSortingOrder.Float;
  53. SetMaskAlpha(0.6f);
  54. }
  55. public static void AddChildToBottomLayer(GObject gObject)
  56. {
  57. _bottomLayer.AddChild(gObject);
  58. }
  59. public static void AddChildToTopLayer(GObject gObject)
  60. {
  61. _topLayer.AddChild(gObject);
  62. }
  63. public static void AddChildToGuideLayer(GObject gObject)
  64. {
  65. _guideLayer.AddChild(gObject);
  66. }
  67. public static void AddChildToModalLayer(GObject gObject)
  68. {
  69. _modalLayer.AddChild(gObject);
  70. }
  71. public static void AddChildToAlertLayer(GObject gObject)
  72. {
  73. _alertLayer.AddChild(gObject);
  74. }
  75. public static void AddChildToDebugLayer(GObject gObject)
  76. {
  77. _debugLayer.AddChild(gObject);
  78. }
  79. public static void AddChildToFloatLayer(GObject gObject)
  80. {
  81. _floatLayer.AddChild(gObject);
  82. }
  83. /// <summary>
  84. /// ��ʾһ����ͼ
  85. /// </summary>
  86. /// <param name="name">Ҫ��ʾ����ͼ����</param>
  87. /// <param name="viewData">Ҫ���ݸ���ͼ�IJ���</param>
  88. /// <param name="goBackParams">�Ӹ���ͼ���ص���ͼ��Ϣ</param>
  89. /// <param name="hideOthers">�Ƿ�ر�������ͼ</param>
  90. public static bool Show(string viewName, object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false)
  91. {
  92. string name = GetName(viewName);
  93. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  94. {
  95. return false;
  96. }
  97. if (hideOthers)
  98. {
  99. HideAllView(name);
  100. }
  101. UIView obj = null;
  102. if (_viewDic.ContainsKey(name))
  103. {
  104. obj = _viewDic[name];
  105. }
  106. else
  107. {
  108. obj = CreateViewInstance(viewName) as UIView;
  109. obj.viewName = name;
  110. _viewDic.Add(name, obj);
  111. }
  112. if (obj != null)
  113. {
  114. IUIView view = (IUIView)obj;
  115. view.viewData = viewData;
  116. if (!view.isShowing)
  117. {
  118. view.Show();
  119. }
  120. else
  121. {
  122. view.Refresh();
  123. }
  124. if (resetGobackParams)
  125. {
  126. if (_goBackDatas.ContainsKey(name) == true)
  127. {
  128. _goBackDatas.Remove(name);
  129. }
  130. }
  131. if (goBackParams != null)
  132. {
  133. _goBackDatas[name] = goBackParams;
  134. }
  135. Debug.Log("当前打开:" + name);
  136. }
  137. return true;
  138. }
  139. public static bool isViewOpen(string viewName)
  140. {
  141. string name = GetName(viewName);
  142. UIView obj = null;
  143. if (_viewDic.ContainsKey(name))
  144. {
  145. obj = _viewDic[name];
  146. if (obj != null)
  147. {
  148. IUIView view = (IUIView)obj;
  149. if (view.isShowing) return true;
  150. }
  151. }
  152. return false;
  153. }
  154. public static bool Show<T>(object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false) where T : class, new()
  155. {
  156. // string[] names = typeof(T).FullName.Split('.');
  157. // string viewName = names[names.Length - 1];
  158. string name = GetName(typeof(T).FullName);
  159. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  160. {
  161. return false;
  162. }
  163. if (hideOthers)
  164. {
  165. HideAllView(name);
  166. }
  167. UIView obj = null;
  168. if (_viewDic.ContainsKey(name))
  169. {
  170. obj = _viewDic[name];
  171. }
  172. else
  173. {
  174. obj = new T() as UIView;
  175. obj.viewName = name;
  176. _viewDic.Add(name, obj);
  177. }
  178. if (obj != null)
  179. {
  180. IUIView view = (IUIView)obj;
  181. view.viewData = viewData;
  182. if (!view.isShowing)
  183. {
  184. view.Show();
  185. }
  186. else
  187. {
  188. view.Refresh();
  189. }
  190. if (goBackParams != null)
  191. {
  192. _goBackDatas[name] = goBackParams;
  193. }
  194. else if (resetGobackParams)
  195. {
  196. if (_goBackDatas.ContainsKey(name) == true)
  197. {
  198. _goBackDatas.Remove(name);
  199. }
  200. }
  201. Debug.Log("当前打开:" + name);
  202. }
  203. return true;
  204. }
  205. public static void Hide(string viewName)
  206. {
  207. string name = GetName(viewName);
  208. if (!_viewDic.ContainsKey(name))
  209. {
  210. return;
  211. }
  212. UIView obj = _viewDic[name];
  213. if (obj != null && obj.isShowing)
  214. {
  215. IUIView view = (IUIView)obj;
  216. view.Hide();
  217. }
  218. }
  219. public static void Hide<T>()
  220. {
  221. string name = GetName(typeof(T).FullName);
  222. if (!_viewDic.ContainsKey(name))
  223. {
  224. return;
  225. }
  226. object obj = _viewDic[name];
  227. if (obj != null)
  228. {
  229. IUIView view = (IUIView)obj;
  230. view.Hide();
  231. }
  232. }
  233. public static void GoBackFrom(string viewName, bool clearGoBackDatas = false)
  234. {
  235. string name = GetName(viewName);
  236. ViewManager.Hide(name);
  237. if (_goBackDatas.ContainsKey(name))
  238. {
  239. object[] gobackItems = _goBackDatas[name];
  240. string tViewName = gobackItems[0] as string;
  241. object tViewData = null;
  242. if (gobackItems.Length > 1)
  243. {
  244. tViewData = gobackItems[1];
  245. }
  246. ViewManager.Show(tViewName, tViewData);
  247. }
  248. else
  249. {
  250. ViewManager.Show(ViewName.MAINUI_VIEW, null, null, true);
  251. }
  252. }
  253. public static object[] GetGoBackDatas(string viewName)
  254. {
  255. string name = GetName(viewName);
  256. object[] value;
  257. _goBackDatas.TryGetValue(name, out value);
  258. return value;
  259. }
  260. public static UIView GetUIView(string viewName)
  261. {
  262. UIView obj = _viewDic[viewName];
  263. if (obj != null && obj.isShowing)
  264. {
  265. return obj;
  266. }
  267. return null;
  268. }
  269. public static void ClearUIView(string viewName)
  270. {
  271. if (!string.IsNullOrEmpty(viewName) && _viewDic.ContainsKey(viewName))
  272. {
  273. if (_viewDic[viewName] != null && !_viewDic[viewName].isShowing)
  274. {
  275. // _viewDic[viewName] = null;
  276. _viewDic.Remove(viewName);
  277. }
  278. }
  279. }
  280. public static void HideAllView(string excludeViewName = null)
  281. {
  282. for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
  283. {
  284. int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
  285. KeyValuePair<string, UIView> kv = _viewDic.ElementAt(index);
  286. if (kv.Key != excludeViewName)
  287. {
  288. if (kv.Key == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  289. Hide(kv.Key);
  290. }
  291. }
  292. // _viewDic.Clear();
  293. // foreach (string viewName in _viewDic.Keys)
  294. // {
  295. // if (viewName != excludeViewName)
  296. // {
  297. // if (viewName == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  298. // Hide(viewName);
  299. // }
  300. // }
  301. }
  302. public static void CheckDispsoe()
  303. {
  304. for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
  305. {
  306. int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
  307. KeyValuePair<string, UIView> kv = _viewDic.ElementAt(index);
  308. if (kv.Value.isShowing == true) continue;
  309. if (kv.Value.packageName == ResPathUtil.GetUIPackagePath("CommonGame") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Common") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
  310. long currentTime = TimeHelper.ClientNowSeconds();
  311. long closeTime = kv.Value.closeTime;
  312. if (closeTime > 0 && currentTime - closeTime >= TimeUtil.SECOND_PER_MUNITE * 0.05)
  313. {
  314. kv.Value.closeTime = 0;
  315. kv.Value.Dispose();
  316. }
  317. }
  318. }
  319. private static object CreateViewInstance(string name)
  320. {
  321. // Debug.LogFormat("CreateViewInstance {0}", name);
  322. Type type = Type.GetType(name);
  323. if (type != null)
  324. {
  325. return Activator.CreateInstance(type);
  326. }
  327. return null;
  328. }
  329. private static GComponent CreateLayer(string name)
  330. {
  331. GComponent layer = new GComponent();
  332. layer.name = name;
  333. GRoot.inst.AddChild(layer);
  334. layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
  335. layer.AddRelation(GRoot.inst, RelationType.Size);
  336. return layer;
  337. }
  338. public static bool CheckIsTopView(GComponent viewCom)
  339. {
  340. if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
  341. if (viewCom.parent != null)
  342. {
  343. int index = viewCom.parent.GetChildIndex(viewCom);
  344. if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
  345. {
  346. return true;
  347. }
  348. }
  349. if (GRoot.inst.GetTopWindow() == viewCom)
  350. {
  351. return true;
  352. }
  353. return false;
  354. }
  355. public static string GetName(string fullName)
  356. {
  357. string[] names = fullName.Split('.');
  358. string name = names[names.Length - 1];
  359. return name;
  360. }
  361. public static void SetMaskAlpha(float alpha)
  362. {
  363. GRoot.inst.modalLayer.alpha = alpha;
  364. }
  365. }
  366. }