ViewManager.cs 18 KB

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