ViewManager.cs 23 KB

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