ViewManager.cs 17 KB

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