ViewManager.cs 18 KB

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