ViewManager.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. using System.Collections.Generic;
  2. using System;
  3. using FairyGUI;
  4. using System.Linq;
  5. using ET;
  6. using UnityEngine;
  7. namespace GFGGame
  8. {
  9. public class ViewStructure
  10. {
  11. public string name;
  12. public object viewData;
  13. public IUIView iUIView;
  14. public bool backRefresh;
  15. }
  16. /// <summary>
  17. /// 视图管理类
  18. /// 管理视图的显示、隐藏
  19. /// </summary>
  20. public class ViewManager
  21. {
  22. private static List<ViewStructure> _viewStack;
  23. private static Dictionary<string, IUIView> _viewDic;
  24. private static GComponent _bottomLayer;
  25. private static GComponent _topLayer;
  26. private static GComponent _guideLayer;
  27. private static GComponent _modalLayer;
  28. private static GComponent _alertLayer;
  29. private static GComponent _debugLayer;
  30. private static GComponent _floatLayer;
  31. private static bool _nowHideOthers = false; //正在关闭所有界面的循环中
  32. private static Dictionary<string, List<object[]>> _goBackDatas = new Dictionary<string, List<object[]>>();
  33. public static void Clear()
  34. {
  35. _viewStack.Clear();
  36. }
  37. public static void Init()
  38. {
  39. //设置CustomLoader
  40. UIObjectFactory.SetLoaderExtension(typeof(GFGGLoader));
  41. //通用资源,单独加,增加一次引用,不会被释放
  42. GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("Common"));
  43. GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("CommonGame"));
  44. GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("Main"));
  45. //await GFGUIPackage.AddPackageAsync(ResPathUtil.GetUIPackagePath("Common"));
  46. UIConfig.buttonSound = (NAudioClip)UIPackage.GetItemAsset("Common", "click");
  47. //统一修改文字字体需要填写路径 ResIn/Font/FZKTJW--GB1-0
  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 = ConstViewLayerSortingOrder.TOP;
  58. _guideLayer = CreateLayer("GuideLayer");
  59. _guideLayer.sortingOrder = ConstViewLayerSortingOrder.Guide;
  60. _modalLayer = CreateLayer("ModalLayer");
  61. _modalLayer.sortingOrder = ConstViewLayerSortingOrder.Modal;
  62. _alertLayer = CreateLayer("AlertLayer");
  63. _alertLayer.sortingOrder = ConstViewLayerSortingOrder.Alert;
  64. //debug层
  65. _debugLayer = CreateLayer("DebugLayer");
  66. _debugLayer.sortingOrder = ConstViewLayerSortingOrder.Debug;
  67. _floatLayer = CreateLayer("FloatLayer");
  68. _floatLayer.sortingOrder = ConstViewLayerSortingOrder.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. public static float ViewWidth
  100. {
  101. get
  102. {
  103. //这里做了最大宽度适配
  104. float maxAspectRatio = 1080 * 1.0f / 1920;
  105. if (Screen.width * 1.0f / Screen.height > maxAspectRatio)
  106. {
  107. return GRoot.inst.height * maxAspectRatio;
  108. }
  109. return GRoot.inst.width;
  110. }
  111. }
  112. /// <summary>
  113. /// 显示一个视图
  114. /// </summary>
  115. /// <param name="viewName">要显示的视图名称</param>
  116. /// <param name="viewData">要传递给视图的参数</param>
  117. /// <param name="goBackParams">从该视图返回的视图信息</param>
  118. /// <param name="hideOthers">是否关闭其他视图</param>
  119. /// <param name="backRefresh">返回上一个界面的时候,上一个界面是否需要刷新界面</param>
  120. public static bool Show(string fullViewName, object viewData = null, bool hideOthers = false, bool backRefresh = true,bool isHideToShow = false)
  121. {
  122. string name = GetName(fullViewName);
  123. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  124. {
  125. return false;
  126. }
  127. if (hideOthers)
  128. {
  129. HideAllView(name);
  130. }
  131. IUIView obj = null;
  132. if (_viewDic.ContainsKey(name))
  133. {
  134. obj = _viewDic[name];
  135. }
  136. else
  137. {
  138. obj = CreateViewInstance(fullViewName) as IUIView;
  139. obj.viewName = name;
  140. _viewDic.Add(name, obj);
  141. }
  142. if (obj != null)
  143. {
  144. IUIView view = (IUIView)obj;
  145. view.viewData = viewData;
  146. if (!view.isShowing)
  147. {
  148. if (isHideToShow && _viewStack.Count > 0){
  149. view.backRefresh = _viewStack[_viewStack.Count - 1].backRefresh;
  150. _viewStack[_viewStack.Count - 1].iUIView = obj;
  151. }
  152. else
  153. view.backRefresh = true;
  154. view.Show();
  155. }
  156. else
  157. {
  158. view.Refresh();
  159. }
  160. LogUtil.LogDev("当前打开:" + name);
  161. }
  162. if (name == "MainUIView")
  163. {
  164. _viewStack.Clear();
  165. }
  166. //判断是否需要保存界面数据, 会帮助关闭上一个保存界面
  167. if (obj.isReturnView && (_viewStack.Count <= 0 || (_viewStack.Count > 0 && _viewStack[_viewStack.Count - 1].name != name)))
  168. {
  169. //保存上一个界面是否需要返回刷新
  170. if (!isHideToShow && _viewStack.Count > 1)
  171. _viewStack[_viewStack.Count - 1].backRefresh = backRefresh;
  172. ViewStructure viewStructure = new ViewStructure();
  173. viewStructure.name = name;
  174. viewStructure.viewData = viewData;
  175. viewStructure.iUIView = obj;
  176. _viewStack.Add(viewStructure);
  177. if (_viewStack.Count > 1 && !hideOthers) {
  178. _viewStack[_viewStack.Count - 2].iUIView.Hide();
  179. }
  180. }
  181. return true;
  182. }
  183. //
  184. /// <summary>
  185. /// 界面可返回栈里的跳转
  186. /// </summary>
  187. /// <param name="viewName">跳转到界面缓存栈里的某个界面,使用这个参数时写在打开下一个界面后</param>
  188. /// <param name="count">删除队列中的倒数几个,较灵活应对更多种情况,可自由控制</param>
  189. public static void DeleteViewStackCountDown(string viewName, int count = 0)
  190. {
  191. if (viewName != null && viewName != "")
  192. {
  193. for (int i = _viewStack.Count - 2; i > 0; i--)
  194. {
  195. ViewStructure viewStructure = _viewStack[i];
  196. if (viewStructure.name == viewName)
  197. break;
  198. _viewStack.RemoveAt(i);
  199. }
  200. return;
  201. }
  202. for (int i = 0; i < count; i++) {
  203. if (_viewStack.Count <= 1)
  204. break;
  205. _viewStack.RemoveAt(_viewStack.Count-1);
  206. }
  207. }
  208. public static bool isViewOpen(string fullViewName)
  209. {
  210. string name = GetName(fullViewName);
  211. IUIView obj = null;
  212. if (_viewDic.ContainsKey(name))
  213. {
  214. obj = _viewDic[name];
  215. if (obj != null)
  216. {
  217. IUIView view = (IUIView)obj;
  218. if (view.isShowing) return true;
  219. }
  220. }
  221. return false;
  222. }
  223. public static bool Show<T>(object viewData = null, bool hideOthers = false, bool backRefresh = true) where T : class, new()
  224. {
  225. // string[] names = typeof(T).FullName.Split('.');
  226. // string viewName = names[names.Length - 1];
  227. //string name = GetName(typeof(T).FullName);
  228. return ViewManager.Show(typeof(T).FullName, viewData, hideOthers, backRefresh);
  229. }
  230. public static void HideWin(string viewName)
  231. {
  232. if (_nowHideOthers)
  233. return;
  234. if (_viewStack.Count >= 1)
  235. {
  236. bool hasShowingView = false;
  237. bool needShowNextView = false;
  238. bool backRefresh = true;
  239. foreach (var info in _viewDic.Keys)
  240. {
  241. IUIView objIsShowing = _viewDic[info];
  242. if (objIsShowing != null && objIsShowing.isShowing)
  243. {
  244. hasShowingView = true;
  245. break;
  246. }
  247. }
  248. ViewStructure viewStructure = _viewStack[_viewStack.Count - 1];
  249. if (_viewStack.Count == 1)
  250. {
  251. //没有界面显示了,栈被清除剩1个的时候,做保底
  252. if (!hasShowingView)
  253. needShowNextView = true;
  254. }
  255. else {
  256. if (!hasShowingView || (viewStructure.iUIView.isReturnView && viewStructure.name == viewName))
  257. {
  258. //关闭自己,在队列里去除
  259. if (viewStructure.iUIView.isReturnView && viewStructure.name == viewName) {
  260. backRefresh = viewStructure.backRefresh;
  261. _viewStack.RemoveAt(_viewStack.Count - 1);
  262. }
  263. if (_viewStack.Count >= 1)
  264. needShowNextView = true;
  265. }
  266. }
  267. if (needShowNextView) {
  268. viewStructure = _viewStack[_viewStack.Count - 1];
  269. ViewManager.Show($"GFGGame.{viewStructure.name}", viewStructure.viewData, false, backRefresh, true);
  270. //重新打开小弹窗
  271. foreach (var objName in _viewDic.Keys)
  272. {
  273. if (objName != viewStructure.name)
  274. {
  275. IUIView view = (IUIView)_viewDic[objName];
  276. if (view.isShowing)
  277. view.Show();
  278. }
  279. }
  280. }
  281. }
  282. }
  283. public static void Hide(string fullViewName)
  284. {
  285. string name = GetName(fullViewName);
  286. if (!_viewDic.ContainsKey(name))
  287. {
  288. return;
  289. }
  290. object obj = _viewDic[name];
  291. if (obj != null)
  292. {
  293. IUIView view = (IUIView)obj;
  294. view.Hide();
  295. LogUtil.LogDev("当前关闭:" + name);
  296. }
  297. }
  298. public static void Hide<T>()
  299. {
  300. //string name = GetName(typeof(T).FullName);
  301. Hide(typeof(T).FullName);
  302. }
  303. public static void GoBackFrom(string fullViewName, bool hideOther = true)
  304. {
  305. string name = GetName(fullViewName);
  306. ViewManager.Hide(name);
  307. foreach (var info in _viewDic.Keys)
  308. {
  309. IUIView objIsShowing = _viewDic[info];
  310. if (objIsShowing != null && objIsShowing.isShowing)
  311. {
  312. return;
  313. }
  314. }
  315. MainDataManager.Instance.ViewType = 0;
  316. ViewManager.Show<MainUIView>(null, true);
  317. }
  318. public static object[] GetGoBackDatas(string fullViewName)
  319. {
  320. //string name = GetName(fullViewName);
  321. object[] value = null;
  322. //if (_goBackDatas.ContainsKey(name) && _goBackDatas[name].Count > 0)
  323. //{
  324. // value = _goBackDatas[name][_goBackDatas[name].Count - 1];
  325. //}
  326. return value;
  327. }
  328. public static IUIView GetUIView(string viewName)
  329. {
  330. if (_viewDic.ContainsKey(viewName))
  331. {
  332. IUIView obj = _viewDic[viewName];
  333. if (obj != null && obj.isShowing)
  334. {
  335. return obj as IUIView;
  336. }
  337. }
  338. return null;
  339. }
  340. public static void ClearUIView(string viewName)
  341. {
  342. if (!string.IsNullOrEmpty(viewName) && _viewDic.ContainsKey(viewName))
  343. {
  344. if (_viewDic[viewName] != null && !_viewDic[viewName].isShowing)
  345. {
  346. // _viewDic[viewName] = null;
  347. _viewDic.Remove(viewName);
  348. }
  349. }
  350. }
  351. public static void HideAllView(string excludeViewName = null)
  352. {
  353. for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
  354. {
  355. int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
  356. KeyValuePair<string, IUIView> kv = _viewDic.ElementAt(index);
  357. if (kv.Key != excludeViewName)
  358. {
  359. if (kv.Key == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  360. _nowHideOthers = true;
  361. Hide(kv.Key);
  362. }
  363. }
  364. _nowHideOthers = false;
  365. // _viewDic.Clear();
  366. // foreach (string viewName in _viewDic.Keys)
  367. // {
  368. // if (viewName != excludeViewName)
  369. // {
  370. // if (viewName == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  371. // Hide(viewName);
  372. // }
  373. // }
  374. }
  375. public static void CheckDispose()
  376. {
  377. for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
  378. {
  379. int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
  380. KeyValuePair<string, IUIView> kv = _viewDic.ElementAt(index);
  381. if (kv.Value.isShowing == true) continue;
  382. // if (kv.Value.packageName == ResPathUtil.GetUIPackagePath("CommonGame") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Common") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
  383. if(_viewDic.Keys.Count <= 10) return; //打开界面小于10个就不销毁了
  384. long currentTime = TimeHelper.ClientNowSeconds();
  385. long closeTime = kv.Value.closeTime;
  386. if (closeTime > 0 && currentTime - closeTime >= TimeUtil.SECOND_PER_MUNITE * 1)
  387. {
  388. kv.Value.closeTime = 0;
  389. kv.Value.Dispose();
  390. }
  391. }
  392. }
  393. private static object CreateViewInstance(string name)
  394. {
  395. //LogUtil.LogFormatDev("CreateViewInstance {0}", name);
  396. Type type = Type.GetType(name);
  397. if (type != null)
  398. {
  399. return Activator.CreateInstance(type);
  400. }
  401. return null;
  402. }
  403. private static GComponent CreateLayer(string name)
  404. {
  405. GComponent layer = new GComponent();
  406. layer.name = name;
  407. GRoot.inst.AddChild(layer);
  408. layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
  409. layer.AddRelation(GRoot.inst, RelationType.Size);
  410. return layer;
  411. }
  412. public static bool CheckIsTopView(GComponent viewCom)
  413. {
  414. if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
  415. if (viewCom.parent != null)
  416. {
  417. int index = viewCom.parent.GetChildIndex(viewCom);
  418. if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
  419. {
  420. return true;
  421. }
  422. }
  423. if (GRoot.inst.GetTopWindow() == viewCom)
  424. {
  425. return true;
  426. }
  427. return false;
  428. }
  429. public static string GetName(string fullName)
  430. {
  431. string[] names = fullName.Split('.');
  432. string name = names[names.Length - 1];
  433. return name;
  434. }
  435. public static void SetMaskAlpha(float alpha)
  436. {
  437. GRoot.inst.modalLayer.alpha = alpha;
  438. }
  439. /// <summary>
  440. /// 任务界面跳转
  441. /// </summary>
  442. /// <param name="jumpId"></param>
  443. public static void JumpToView(string jumpId, object[] param, bool hideOther = false, bool backRefresh = true, Action onSuccess = null)
  444. {
  445. switch (jumpId)
  446. {
  447. case nameof(LeagueAnswerView):
  448. if (LeagueDataManager.Instance.Type == LeagueJoinType.Join)
  449. {
  450. ViewManager.Show<LeagueView>(null, hideOther, backRefresh);
  451. ViewManager.Show($"GFGGame.{jumpId}");
  452. }
  453. else
  454. {
  455. if (!FunctionOpenDataManager.Instance.CheckIsFunOpenById(nameof(LeagueView))) return;
  456. ViewManager.Show<LeagueJoinView>(null, hideOther, backRefresh);
  457. }
  458. break;
  459. case nameof(LeagueView):
  460. if (!FunctionOpenDataManager.Instance.CheckIsFunOpenById(nameof(LeagueView))) return;
  461. if (LeagueDataManager.Instance.Type == LeagueJoinType.Join)
  462. {
  463. ViewManager.Show<LeagueView>(null, hideOther, backRefresh);
  464. }
  465. else
  466. {
  467. ViewManager.Show<LeagueJoinView>(null, hideOther, backRefresh);
  468. }
  469. break;
  470. case nameof(StoreView):
  471. ViewManager.Show<StoreView>(param, hideOther, backRefresh);
  472. break;
  473. case nameof(StoryChapterListView):
  474. ViewManager.Show($"GFGGame.{jumpId}", param, hideOther, backRefresh);
  475. break;
  476. case nameof(StoryChapterView):
  477. ViewManager.Show<StoryChapterView>(param[0], hideOther, backRefresh);
  478. break;
  479. case nameof(FirstChargeBonusView):
  480. ViewManager.Show<FirstChargeBonusView>(param, false, backRefresh);
  481. break;
  482. case nameof(ClothingSyntheticView):
  483. ViewManager.Show<ClothingSyntheticView>(param, hideOther, backRefresh);
  484. break;
  485. case nameof(LuckyBoxView):
  486. if(param.Length > 0)
  487. ViewManager.Show<LuckyBoxView>(param[0], hideOther, backRefresh);
  488. else
  489. ViewManager.Show<LuckyBoxView>(null, hideOther, backRefresh);
  490. break;
  491. default:
  492. ViewManager.Show($"GFGGame.{jumpId}", null, hideOther, backRefresh);
  493. break;
  494. }
  495. onSuccess?.Invoke();
  496. }
  497. }
  498. }