ViewManager.cs 15 KB

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