ViewManager.cs 16 KB

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