ViewManager.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. using System.Collections.Generic;
  2. using System;
  3. using FairyGUI;
  4. using System.Linq;
  5. using ET;
  6. using UnityEngine;
  7. namespace GFGGame
  8. {
  9. public class ViewStructure
  10. {
  11. public string name;
  12. public object viewData;
  13. public IUIView iUIView;
  14. public bool backRefresh;
  15. public List<String> smallWindow;
  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 = ConstViewLayerSortingOrder.TOP;
  59. _guideLayer = CreateLayer("GuideLayer");
  60. _guideLayer.sortingOrder = ConstViewLayerSortingOrder.Guide;
  61. _modalLayer = CreateLayer("ModalLayer");
  62. _modalLayer.sortingOrder = ConstViewLayerSortingOrder.Modal;
  63. _alertLayer = CreateLayer("AlertLayer");
  64. _alertLayer.sortingOrder = ConstViewLayerSortingOrder.Alert;
  65. //debug层
  66. _debugLayer = CreateLayer("DebugLayer");
  67. _debugLayer.sortingOrder = ConstViewLayerSortingOrder.Debug;
  68. _floatLayer = CreateLayer("FloatLayer");
  69. _floatLayer.sortingOrder = ConstViewLayerSortingOrder.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. public static float ViewWidth
  101. {
  102. get
  103. {
  104. //这里做了最大宽度适配
  105. float maxAspectRatio = 1080 * 1.0f / 1920;
  106. if (Screen.width * 1.0f / Screen.height > maxAspectRatio)
  107. {
  108. return GRoot.inst.height * maxAspectRatio;
  109. }
  110. return GRoot.inst.width;
  111. }
  112. }
  113. /// <summary>
  114. /// 显示一个视图
  115. /// </summary>
  116. /// <param name="viewName">要显示的视图名称</param>
  117. /// <param name="viewData">要传递给视图的参数</param>
  118. /// <param name="goBackParams">从该视图返回的视图信息</param>
  119. /// <param name="hideOthers">是否关闭其他视图</param>
  120. /// <param name="backRefresh">返回上一个界面的时候,上一个界面是否需要刷新界面</param>
  121. public static bool Show(string fullViewName, object viewData = null, bool hideOthers = false, bool backRefresh = true,bool isHideToShow = false)
  122. {
  123. string name = GetName(fullViewName);
  124. LogUtil.LogDev("当前打开:" + name);
  125. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  126. {
  127. return false;
  128. }
  129. if (hideOthers)
  130. {
  131. HideAllView(name);
  132. }
  133. IUIView obj = null;
  134. if (_viewDic.ContainsKey(name))
  135. {
  136. obj = _viewDic[name];
  137. }
  138. else
  139. {
  140. obj = CreateViewInstance(fullViewName) as IUIView;
  141. obj.viewName = name;
  142. _viewDic.Add(name, obj);
  143. }
  144. if (obj != null)
  145. {
  146. IUIView view = (IUIView)obj;
  147. view.viewData = viewData;
  148. if (!view.isShowing)
  149. {
  150. if (isHideToShow && _viewStack.Count > 0){
  151. view.backRefresh = _viewStack[_viewStack.Count - 1].backRefresh;
  152. _viewStack[_viewStack.Count - 1].iUIView = obj;
  153. }
  154. else
  155. view.backRefresh = true;
  156. view.Show();
  157. }
  158. else
  159. {
  160. view.Refresh();
  161. }
  162. LogUtil.LogDev("当前打开:" + name);
  163. }
  164. if (name == "MainUIView")
  165. {
  166. _viewStack.Clear();
  167. }
  168. //判断是否需要保存界面数据, 会帮助关闭上一个保存界面
  169. if (obj.isReturnView && (_viewStack.Count <= 0 || (_viewStack.Count > 0 && _viewStack[_viewStack.Count - 1].name != name)))
  170. {
  171. //保存上一个界面是否需要返回刷新
  172. if (!isHideToShow && _viewStack.Count > 1)
  173. _viewStack[_viewStack.Count - 1].backRefresh = backRefresh;
  174. ViewStructure viewStructure = new ViewStructure();
  175. viewStructure.name = name;
  176. viewStructure.viewData = viewData;
  177. viewStructure.iUIView = obj;
  178. _viewStack.Add(viewStructure);
  179. if (_viewStack.Count > 1 && !hideOthers) {
  180. if (_viewStack[_viewStack.Count - 2].smallWindow == null)
  181. _viewStack[_viewStack.Count - 2].smallWindow = new List<string>();
  182. _viewStack[_viewStack.Count - 2].smallWindow.Clear();
  183. //把开着的小弹窗存起来
  184. foreach (var objName in _viewDic.Keys)
  185. {
  186. IUIView view = (IUIView)_viewDic[objName];
  187. if (view.isReturnWindow)
  188. {
  189. if (view.isShowing)
  190. {
  191. _viewStack[_viewStack.Count - 2].smallWindow.Add(objName);
  192. view.Hide();
  193. }
  194. }
  195. }
  196. _viewStack[_viewStack.Count - 2].iUIView.Hide();
  197. }
  198. }
  199. return true;
  200. }
  201. //
  202. /// <summary>
  203. /// 界面可返回栈里的跳转
  204. /// </summary>
  205. /// <param name="viewName">跳转到界面缓存栈里的某个界面,使用这个参数时写在打开下一个界面后</param>
  206. /// <param name="count">删除队列中的倒数几个,较灵活应对更多种情况,可自由控制</param>
  207. public static void DeleteViewStackCountDown(string viewName, int count = 0)
  208. {
  209. if (viewName != null && viewName != "")
  210. {
  211. for (int i = _viewStack.Count - 2; i > 0; i--)
  212. {
  213. ViewStructure viewStructure = _viewStack[i];
  214. if (viewStructure.name == viewName)
  215. break;
  216. _viewStack.RemoveAt(i);
  217. }
  218. return;
  219. }
  220. for (int i = 0; i < count; i++) {
  221. if (_viewStack.Count <= 1)
  222. break;
  223. _viewStack.RemoveAt(_viewStack.Count-1);
  224. }
  225. }
  226. public static bool isViewOpen(string fullViewName)
  227. {
  228. string name = GetName(fullViewName);
  229. IUIView obj = null;
  230. if (_viewDic.ContainsKey(name))
  231. {
  232. obj = _viewDic[name];
  233. if (obj != null)
  234. {
  235. IUIView view = (IUIView)obj;
  236. if (view.isShowing) return true;
  237. }
  238. }
  239. return false;
  240. }
  241. public static bool Show<T>(object viewData = null, bool hideOthers = false, bool backRefresh = true) where T : class, new()
  242. {
  243. // string[] names = typeof(T).FullName.Split('.');
  244. // string viewName = names[names.Length - 1];
  245. //string name = GetName(typeof(T).FullName);
  246. return ViewManager.Show(typeof(T).FullName, viewData, hideOthers, backRefresh);
  247. }
  248. public static void HideWin(string viewName)
  249. {
  250. if (_nowHideOthers)
  251. return;
  252. if (_viewStack.Count >= 1)
  253. {
  254. bool hasShowingView = false;
  255. bool needShowNextView = false;
  256. bool backRefresh = true;
  257. foreach (var info in _viewDic.Keys)
  258. {
  259. IUIView objIsShowing = _viewDic[info];
  260. if (objIsShowing != null && objIsShowing.isShowing)
  261. {
  262. hasShowingView = true;
  263. break;
  264. }
  265. }
  266. ViewStructure viewStructure = _viewStack[_viewStack.Count - 1];
  267. if (_viewStack.Count == 1)
  268. {
  269. //没有界面显示了,栈被清除剩1个的时候,做保底
  270. if (!hasShowingView)
  271. needShowNextView = true;
  272. }
  273. else {
  274. if (!hasShowingView || (viewStructure.iUIView.isReturnView && viewStructure.name == viewName))
  275. {
  276. //关闭自己,在队列里去除
  277. if (viewStructure.iUIView.isReturnView && viewStructure.name == viewName) {
  278. backRefresh = viewStructure.backRefresh;
  279. _viewStack.RemoveAt(_viewStack.Count - 1);
  280. }
  281. if (_viewStack.Count >= 1)
  282. needShowNextView = true;
  283. }
  284. }
  285. if (needShowNextView) {
  286. viewStructure = _viewStack[_viewStack.Count - 1];
  287. ViewManager.Show($"GFGGame.{viewStructure.name}", viewStructure.viewData, false, backRefresh, true);
  288. //重新打开小弹窗
  289. if (viewStructure.smallWindow != null) {
  290. foreach (var objName in viewStructure.smallWindow)
  291. {
  292. ViewManager.Show($"GFGGame.{objName}");
  293. }
  294. }
  295. //foreach (var objName in _viewDic.Keys)
  296. //{
  297. // if (objName != viewStructure.name)
  298. // {
  299. // IUIView view = (IUIView)_viewDic[objName];
  300. // if (view.isShowing)
  301. // view.Show();
  302. // }
  303. //}
  304. }
  305. }
  306. }
  307. public static void Hide(string fullViewName)
  308. {
  309. string name = GetName(fullViewName);
  310. if (!_viewDic.ContainsKey(name))
  311. {
  312. return;
  313. }
  314. object obj = _viewDic[name];
  315. if (obj != null)
  316. {
  317. IUIView view = (IUIView)obj;
  318. view.Hide();
  319. LogUtil.LogDev("当前关闭:" + name);
  320. }
  321. }
  322. public static void Hide<T>()
  323. {
  324. //string name = GetName(typeof(T).FullName);
  325. Hide(typeof(T).FullName);
  326. }
  327. public static void GoBackFrom(string fullViewName, bool hideOther = true)
  328. {
  329. string name = GetName(fullViewName);
  330. ViewManager.Hide(name);
  331. foreach (var info in _viewDic.Keys)
  332. {
  333. IUIView objIsShowing = _viewDic[info];
  334. if (objIsShowing != null && objIsShowing.isShowing)
  335. {
  336. return;
  337. }
  338. }
  339. MainDataManager.Instance.ViewType = 0;
  340. ViewManager.Show<MainUIView>(null, true);
  341. }
  342. public static object[] GetGoBackDatas(string fullViewName)
  343. {
  344. //string name = GetName(fullViewName);
  345. object[] value = null;
  346. //if (_goBackDatas.ContainsKey(name) && _goBackDatas[name].Count > 0)
  347. //{
  348. // value = _goBackDatas[name][_goBackDatas[name].Count - 1];
  349. //}
  350. return value;
  351. }
  352. public static IUIView GetUIView(string viewName)
  353. {
  354. if (_viewDic.ContainsKey(viewName))
  355. {
  356. IUIView obj = _viewDic[viewName];
  357. if (obj != null && obj.isShowing)
  358. {
  359. return obj as IUIView;
  360. }
  361. }
  362. return null;
  363. }
  364. public static void ClearUIView(string viewName)
  365. {
  366. if (!string.IsNullOrEmpty(viewName) && _viewDic.ContainsKey(viewName))
  367. {
  368. if (_viewDic[viewName] != null && !_viewDic[viewName].isShowing)
  369. {
  370. // _viewDic[viewName] = null;
  371. _viewDic.Remove(viewName);
  372. }
  373. }
  374. }
  375. public static void HideAllView(string excludeViewName = null)
  376. {
  377. for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
  378. {
  379. int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
  380. KeyValuePair<string, IUIView> kv = _viewDic.ElementAt(index);
  381. if (kv.Key != excludeViewName)
  382. {
  383. if (kv.Key == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  384. _nowHideOthers = true;
  385. Hide(kv.Key);
  386. }
  387. }
  388. _nowHideOthers = false;
  389. // _viewDic.Clear();
  390. // foreach (string viewName in _viewDic.Keys)
  391. // {
  392. // if (viewName != excludeViewName)
  393. // {
  394. // if (viewName == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  395. // Hide(viewName);
  396. // }
  397. // }
  398. }
  399. public static void CheckDispose()
  400. {
  401. for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
  402. {
  403. int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
  404. KeyValuePair<string, IUIView> kv = _viewDic.ElementAt(index);
  405. if (kv.Value.isShowing == true) continue;
  406. // if (kv.Value.packageName == ResPathUtil.GetUIPackagePath("CommonGame") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Common") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
  407. if(_viewDic.Keys.Count <= 10) return; //打开界面小于10个就不销毁了
  408. long currentTime = TimeHelper.ClientNowSeconds();
  409. long closeTime = kv.Value.closeTime;
  410. if (closeTime > 0 && currentTime - closeTime >= TimeUtil.SECOND_PER_MUNITE * 1)
  411. {
  412. kv.Value.closeTime = 0;
  413. kv.Value.Dispose();
  414. }
  415. }
  416. }
  417. private static object CreateViewInstance(string name)
  418. {
  419. //LogUtil.LogFormatDev("CreateViewInstance {0}", name);
  420. Type type = Type.GetType(name);
  421. if (type != null)
  422. {
  423. return Activator.CreateInstance(type);
  424. }
  425. return null;
  426. }
  427. private static GComponent CreateLayer(string name)
  428. {
  429. GComponent layer = new GComponent();
  430. layer.name = name;
  431. GRoot.inst.AddChild(layer);
  432. layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
  433. layer.AddRelation(GRoot.inst, RelationType.Size);
  434. return layer;
  435. }
  436. public static bool CheckIsTopView(GComponent viewCom)
  437. {
  438. if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
  439. if (viewCom.parent != null)
  440. {
  441. int index = viewCom.parent.GetChildIndex(viewCom);
  442. if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
  443. {
  444. return true;
  445. }
  446. }
  447. if (GRoot.inst.GetTopWindow() == viewCom)
  448. {
  449. return true;
  450. }
  451. return false;
  452. }
  453. public static string GetName(string fullName)
  454. {
  455. string[] names = fullName.Split('.');
  456. string name = names[names.Length - 1];
  457. return name;
  458. }
  459. public static void SetMaskAlpha(float alpha)
  460. {
  461. GRoot.inst.modalLayer.alpha = alpha;
  462. }
  463. /// <summary>
  464. /// 任务界面跳转
  465. /// </summary>
  466. /// <param name="jumpId"></param>
  467. public static void JumpToView(string jumpId, object[] param, bool hideOther = false, bool backRefresh = true, Action onSuccess = null)
  468. {
  469. switch (jumpId)
  470. {
  471. case nameof(LeagueAnswerView):
  472. if (LeagueDataManager.Instance.Type == LeagueJoinType.Join)
  473. {
  474. ViewManager.Show<LeagueView>(null, hideOther, backRefresh);
  475. ViewManager.Show($"GFGGame.{jumpId}");
  476. }
  477. else
  478. {
  479. if (!FunctionOpenDataManager.Instance.CheckIsFunOpenById(nameof(LeagueView))) return;
  480. ViewManager.Show<LeagueJoinView>(null, hideOther, backRefresh);
  481. }
  482. break;
  483. case nameof(LeagueView):
  484. if (!FunctionOpenDataManager.Instance.CheckIsFunOpenById(nameof(LeagueView))) return;
  485. if (LeagueDataManager.Instance.Type == LeagueJoinType.Join)
  486. {
  487. ViewManager.Show<LeagueView>(null, hideOther, backRefresh);
  488. }
  489. else
  490. {
  491. ViewManager.Show<LeagueJoinView>(null, hideOther, backRefresh);
  492. }
  493. break;
  494. case nameof(StoreView):
  495. ViewManager.Show<StoreView>(param, hideOther, backRefresh);
  496. break;
  497. case nameof(StoryChapterListView):
  498. ViewManager.Show($"GFGGame.{jumpId}", param, hideOther, backRefresh);
  499. break;
  500. case nameof(StoryChapterView):
  501. ViewManager.Show<StoryChapterView>(param[0], hideOther, backRefresh);
  502. break;
  503. case nameof(FirstChargeBonusView):
  504. ViewManager.Show<FirstChargeBonusView>(param, false, backRefresh);
  505. break;
  506. case nameof(ClothingSyntheticView):
  507. ViewManager.Show<ClothingSyntheticView>(param, hideOther, backRefresh);
  508. break;
  509. case nameof(LuckyBoxView):
  510. if(param.Length > 0)
  511. ViewManager.Show<LuckyBoxView>(param[0], hideOther, backRefresh);
  512. else
  513. ViewManager.Show<LuckyBoxView>(null, hideOther, backRefresh);
  514. break;
  515. default:
  516. ViewManager.Show($"GFGGame.{jumpId}", null, hideOther, backRefresh);
  517. break;
  518. }
  519. onSuccess?.Invoke();
  520. }
  521. }
  522. }