ViewManager.cs 18 KB

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