ViewManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. return true;
  156. }
  157. //删除队列中的倒数几个
  158. public static void DeleteViewStackCountDown(int count)
  159. {
  160. for (int i = 0; i < count; i++) {
  161. _viewStack.RemoveAt(_viewStack.Count-1);
  162. }
  163. }
  164. public static bool isViewOpen(string fullViewName)
  165. {
  166. string name = GetName(fullViewName);
  167. IUIView obj = null;
  168. if (_viewDic.ContainsKey(name))
  169. {
  170. obj = _viewDic[name];
  171. if (obj != null)
  172. {
  173. IUIView view = (IUIView)obj;
  174. if (view.isShowing) return true;
  175. }
  176. }
  177. return false;
  178. }
  179. public static bool Show<T>(object viewData = null, bool hideOthers = false) where T : class, new()
  180. {
  181. // string[] names = typeof(T).FullName.Split('.');
  182. // string viewName = names[names.Length - 1];
  183. //string name = GetName(typeof(T).FullName);
  184. return ViewManager.Show(typeof(T).FullName, viewData, hideOthers);
  185. }
  186. public static void HideWin(string viewName)
  187. {
  188. if (_viewStack.Count > 1 && !_nowHideOthers) {
  189. ViewStructure viewStructure = _viewStack[_viewStack.Count - 1];
  190. bool hasShowingView = false;
  191. foreach (var info in _viewDic.Keys)
  192. {
  193. IUIView objIsShowing = _viewDic[info];
  194. if (objIsShowing != null && objIsShowing.isShowing)
  195. {
  196. hasShowingView = true;
  197. break;
  198. }
  199. }
  200. if (!hasShowingView || (viewStructure.iUIView.isReturnView && viewStructure.name == viewName))
  201. {
  202. //关闭自己,在队列里去除
  203. if(viewStructure.iUIView.isReturnView && viewStructure.name == viewName)
  204. _viewStack.RemoveAt(_viewStack.Count - 1);
  205. if (_viewStack.Count >= 1)
  206. {
  207. viewStructure = _viewStack[_viewStack.Count - 1];
  208. ViewManager.Show($"GFGGame.{viewStructure.name}", viewStructure.viewData);
  209. foreach (var objName in _viewDic.Keys)
  210. {
  211. if (objName != viewStructure.name)
  212. {
  213. IUIView view = (IUIView)_viewDic[objName];
  214. if (view.isShowing)
  215. {
  216. view.Show();
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. public static void Hide(string fullViewName)
  225. {
  226. string name = GetName(fullViewName);
  227. if (!_viewDic.ContainsKey(name))
  228. {
  229. return;
  230. }
  231. object obj = _viewDic[name];
  232. if (obj != null)
  233. {
  234. IUIView view = (IUIView)obj;
  235. view.Hide();
  236. LogUtil.LogDev("当前关闭:" + name);
  237. }
  238. }
  239. public static void Hide<T>()
  240. {
  241. //string name = GetName(typeof(T).FullName);
  242. Hide(typeof(T).FullName);
  243. }
  244. public static void GoBackFrom(string fullViewName, bool hideOther = true)
  245. {
  246. string name = GetName(fullViewName);
  247. ViewManager.Hide(name);
  248. foreach (var info in _viewDic.Keys)
  249. {
  250. IUIView objIsShowing = _viewDic[info];
  251. if (objIsShowing != null && objIsShowing.isShowing)
  252. {
  253. return;
  254. }
  255. }
  256. MainDataManager.Instance.ViewType = 0;
  257. ViewManager.Show<MainUIView>(null, true);
  258. }
  259. public static object[] GetGoBackDatas(string fullViewName)
  260. {
  261. //string name = GetName(fullViewName);
  262. object[] value = null;
  263. //if (_goBackDatas.ContainsKey(name) && _goBackDatas[name].Count > 0)
  264. //{
  265. // value = _goBackDatas[name][_goBackDatas[name].Count - 1];
  266. //}
  267. return value;
  268. }
  269. public static IUIView GetUIView(string viewName)
  270. {
  271. if (_viewDic.ContainsKey(viewName))
  272. {
  273. IUIView obj = _viewDic[viewName];
  274. if (obj != null && obj.isShowing)
  275. {
  276. return obj as IUIView;
  277. }
  278. }
  279. return null;
  280. }
  281. public static void ClearUIView(string viewName)
  282. {
  283. if (!string.IsNullOrEmpty(viewName) && _viewDic.ContainsKey(viewName))
  284. {
  285. if (_viewDic[viewName] != null && !_viewDic[viewName].isShowing)
  286. {
  287. // _viewDic[viewName] = null;
  288. _viewDic.Remove(viewName);
  289. }
  290. }
  291. }
  292. public static void HideAllView(string excludeViewName = null)
  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.Key != excludeViewName)
  299. {
  300. if (kv.Key == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  301. _nowHideOthers = true;
  302. Hide(kv.Key);
  303. }
  304. }
  305. _nowHideOthers = false;
  306. // _viewDic.Clear();
  307. // foreach (string viewName in _viewDic.Keys)
  308. // {
  309. // if (viewName != excludeViewName)
  310. // {
  311. // if (viewName == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  312. // Hide(viewName);
  313. // }
  314. // }
  315. }
  316. public static void CheckDispose()
  317. {
  318. for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
  319. {
  320. int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
  321. KeyValuePair<string, IUIView> kv = _viewDic.ElementAt(index);
  322. if (kv.Value.isShowing == true) continue;
  323. // if (kv.Value.packageName == ResPathUtil.GetUIPackagePath("CommonGame") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Common") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
  324. if(_viewDic.Keys.Count <= 10) return; //打开界面小于10个就不销毁了
  325. long currentTime = TimeHelper.ClientNowSeconds();
  326. long closeTime = kv.Value.closeTime;
  327. if (closeTime > 0 && currentTime - closeTime >= TimeUtil.SECOND_PER_MUNITE * 1)
  328. {
  329. kv.Value.closeTime = 0;
  330. kv.Value.Dispose();
  331. }
  332. }
  333. }
  334. private static object CreateViewInstance(string name)
  335. {
  336. //LogUtil.LogFormatDev("CreateViewInstance {0}", name);
  337. Type type = Type.GetType(name);
  338. if (type != null)
  339. {
  340. return Activator.CreateInstance(type);
  341. }
  342. return null;
  343. }
  344. private static GComponent CreateLayer(string name)
  345. {
  346. GComponent layer = new GComponent();
  347. layer.name = name;
  348. GRoot.inst.AddChild(layer);
  349. layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
  350. layer.AddRelation(GRoot.inst, RelationType.Size);
  351. return layer;
  352. }
  353. public static bool CheckIsTopView(GComponent viewCom)
  354. {
  355. if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
  356. if (viewCom.parent != null)
  357. {
  358. int index = viewCom.parent.GetChildIndex(viewCom);
  359. if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
  360. {
  361. return true;
  362. }
  363. }
  364. if (GRoot.inst.GetTopWindow() == viewCom)
  365. {
  366. return true;
  367. }
  368. return false;
  369. }
  370. public static string GetName(string fullName)
  371. {
  372. string[] names = fullName.Split('.');
  373. string name = names[names.Length - 1];
  374. return name;
  375. }
  376. public static void SetMaskAlpha(float alpha)
  377. {
  378. GRoot.inst.modalLayer.alpha = alpha;
  379. }
  380. /// <summary>
  381. /// 任务界面跳转
  382. /// </summary>
  383. /// <param name="jumpId"></param>
  384. public static void JumpToView(string jumpId, object[] param, bool hideOther = false, Action onSuccess = null)
  385. {
  386. switch (jumpId)
  387. {
  388. case nameof(LeagueAnswerView):
  389. if (LeagueDataManager.Instance.Type == LeagueJoinType.Join)
  390. {
  391. ViewManager.Show<LeagueView>(null, hideOther);
  392. ViewManager.Show($"GFGGame.{jumpId}");
  393. }
  394. else
  395. {
  396. if (!FunctionOpenDataManager.Instance.CheckIsFunOpenById(nameof(LeagueView))) return;
  397. ViewManager.Show<LeagueJoinView>(null, hideOther);
  398. }
  399. break;
  400. case nameof(LeagueView):
  401. if (!FunctionOpenDataManager.Instance.CheckIsFunOpenById(nameof(LeagueView))) return;
  402. if (LeagueDataManager.Instance.Type == LeagueJoinType.Join)
  403. {
  404. ViewManager.Show<LeagueView>(null, hideOther);
  405. }
  406. else
  407. {
  408. ViewManager.Show<LeagueJoinView>(null, hideOther);
  409. }
  410. break;
  411. case nameof(StoreView):
  412. ViewManager.Show<StoreView>(param, hideOther);
  413. break;
  414. case nameof(StoryChapterListView):
  415. ViewManager.Show($"GFGGame.{jumpId}", param, hideOther);
  416. break;
  417. case nameof(StoryChapterView):
  418. ViewManager.Show<StoryChapterView>(param[0], hideOther);
  419. break;
  420. case nameof(FirstChargeBonusView):
  421. ViewManager.Show<FirstChargeBonusView>(param, false);
  422. break;
  423. case nameof(ClothingSyntheticView):
  424. ViewManager.Show<ClothingSyntheticView>(param, hideOther);
  425. break;
  426. case nameof(LuckyBoxView):
  427. if(param.Length > 0)
  428. ViewManager.Show<LuckyBoxView>(param[0], hideOther);
  429. else
  430. ViewManager.Show<LuckyBoxView>(null, hideOther);
  431. break;
  432. default:
  433. ViewManager.Show($"GFGGame.{jumpId}", null, hideOther);
  434. break;
  435. }
  436. onSuccess?.Invoke();
  437. }
  438. }
  439. }