ViewManager.cs 17 KB

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