ViewManager.cs 19 KB

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