ViewManager.cs 18 KB

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