ViewManager.cs 18 KB

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