ViewManager.cs 18 KB

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