ViewManager.cs 14 KB

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