ViewManager.cs 18 KB

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