ViewManager.cs 17 KB

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