ViewManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. /// <summary>
  10. /// ��ͼ������
  11. /// ������ͼ����ʾ������
  12. /// </summary>
  13. public class ViewManager
  14. {
  15. private static Dictionary<string, UIView> _viewDic;
  16. private static GComponent _bottomLayer;
  17. private static GComponent _topLayer;
  18. private static GComponent _guideLayer;
  19. private static GComponent _modalLayer;
  20. private static GComponent _alertLayer;
  21. private static GComponent _debugLayer;
  22. private static GComponent _floatLayer;
  23. private static Dictionary<string, List<object[]>> _goBackDatas = new Dictionary<string, List<object[]>>();
  24. public static void Init()
  25. {
  26. GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("Common"));
  27. UIConfig.buttonSound = (NAudioClip)UIPackage.GetItemAsset("Common", "click");
  28. Font font0 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("FangZhengHeiTiJianTi-1", "ttf"));
  29. FontManager.RegisterFont(new DynamicFont("FangZhengHeiTiJianTi-1", font0));
  30. UIConfig.defaultFont = "FangZhengHeiTiJianTi-1";
  31. Font font1 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("FZKTJW--GB1-0", "ttf"));
  32. FontManager.RegisterFont(new DynamicFont("FZKTJW--GB1-0", font1));
  33. Font font2 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SourceHanSerifCN-Regular-1", "otf"));
  34. FontManager.RegisterFont(new DynamicFont("SourceHanSerifCN-Regular-1", font2));
  35. Font font3 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SourceHanSerifCN-Bold-2", "otf"));
  36. FontManager.RegisterFont(new DynamicFont("SourceHanSerifCN-Bold-2", font3));
  37. Font font4 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SourceHanSerifCN-ExtraLight-3", "otf"));
  38. FontManager.RegisterFont(new DynamicFont("SourceHanSerifCN-ExtraLight-3", font4));
  39. Font font5 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SourceHanSerifCN-Heavy-4", "otf"));
  40. FontManager.RegisterFont(new DynamicFont("SourceHanSerifCN-Heavy-4", font5));
  41. Font font6 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SourceHanSerifCN-Light-5", "otf"));
  42. FontManager.RegisterFont(new DynamicFont("SourceHanSerifCN-Light-5", font6));
  43. Font font7 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SourceHanSerifCN-Medium-6", "otf"));
  44. FontManager.RegisterFont(new DynamicFont("SourceHanSerifCN-Medium-6", font7));
  45. Font font8 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SourceHanSerifCN-SemiBold-7", "otf"));
  46. FontManager.RegisterFont(new DynamicFont("SourceHanSerifCN-SemiBold-7", font8));
  47. _viewDic = new Dictionary<string, UIView>();
  48. //��ʼ����ͼ������
  49. _bottomLayer = CreateLayer("BottomLayer");
  50. _topLayer = CreateLayer("TopLayer");
  51. _topLayer.sortingOrder = ConstSortingOrder.TOP;
  52. _guideLayer = CreateLayer("GuideLayer");
  53. _guideLayer.sortingOrder = ConstSortingOrder.Guide;
  54. _modalLayer = CreateLayer("ModalLayer");
  55. _modalLayer.sortingOrder = ConstSortingOrder.Modal;
  56. _alertLayer = CreateLayer("AlertLayer");
  57. _alertLayer.sortingOrder = ConstSortingOrder.Alert;
  58. //debug层
  59. _debugLayer = CreateLayer("DebugLayer");
  60. _debugLayer.sortingOrder = ConstSortingOrder.Debug;
  61. _floatLayer = CreateLayer("FloatLayer");
  62. _floatLayer.sortingOrder = ConstSortingOrder.Float;
  63. SetMaskAlpha(0.6f);
  64. }
  65. public static void AddChildToBottomLayer(GObject gObject)
  66. {
  67. _bottomLayer.AddChild(gObject);
  68. }
  69. public static void AddChildToTopLayer(GObject gObject)
  70. {
  71. _topLayer.AddChild(gObject);
  72. }
  73. public static void AddChildToGuideLayer(GObject gObject)
  74. {
  75. _guideLayer.AddChild(gObject);
  76. }
  77. public static void AddChildToModalLayer(GObject gObject)
  78. {
  79. _modalLayer.AddChild(gObject);
  80. }
  81. public static void AddChildToAlertLayer(GObject gObject)
  82. {
  83. _alertLayer.AddChild(gObject);
  84. }
  85. public static void AddChildToDebugLayer(GObject gObject)
  86. {
  87. _debugLayer.AddChild(gObject);
  88. }
  89. public static void AddChildToFloatLayer(GObject gObject)
  90. {
  91. _floatLayer.AddChild(gObject);
  92. }
  93. /// <summary>
  94. /// ��ʾһ����ͼ
  95. /// </summary>
  96. /// <param name="name">Ҫ��ʾ����ͼ����</param>
  97. /// <param name="viewData">Ҫ���ݸ���ͼ�IJ���</param>
  98. /// <param name="goBackParams">�Ӹ���ͼ���ص���ͼ��Ϣ</param>
  99. /// <param name="hideOthers">�Ƿ�ر�������ͼ</param>
  100. public static bool Show(string fullViewName, object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false)
  101. {
  102. string name = GetName(fullViewName);
  103. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  104. {
  105. return false;
  106. }
  107. if (hideOthers)
  108. {
  109. HideAllView(name);
  110. }
  111. UIView obj = null;
  112. if (_viewDic.ContainsKey(name))
  113. {
  114. obj = _viewDic[name];
  115. }
  116. else
  117. {
  118. obj = CreateViewInstance(fullViewName) as UIView;
  119. obj.viewName = name;
  120. _viewDic.Add(name, obj);
  121. }
  122. if (obj != null)
  123. {
  124. IUIView view = (IUIView)obj;
  125. view.viewData = viewData;
  126. if (!view.isShowing)
  127. {
  128. view.Show();
  129. }
  130. else
  131. {
  132. view.Refresh();
  133. }
  134. if (resetGobackParams)
  135. {
  136. if (_goBackDatas.ContainsKey(name) == true)
  137. {
  138. _goBackDatas.Remove(name);
  139. }
  140. }
  141. if (goBackParams != null)
  142. {
  143. if (!_goBackDatas.ContainsKey(name))
  144. {
  145. _goBackDatas.Add(name, new List<object[]>());
  146. }
  147. _goBackDatas[name].Add(goBackParams);
  148. }
  149. Debug.Log("当前打开:" + name);
  150. }
  151. return true;
  152. }
  153. public static bool isViewOpen(string fullViewName)
  154. {
  155. string name = GetName(fullViewName);
  156. UIView obj = null;
  157. if (_viewDic.ContainsKey(name))
  158. {
  159. obj = _viewDic[name];
  160. if (obj != null)
  161. {
  162. IUIView view = (IUIView)obj;
  163. if (view.isShowing) return true;
  164. }
  165. }
  166. return false;
  167. }
  168. public static bool Show<T>(object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false) where T : class, new()
  169. {
  170. // string[] names = typeof(T).FullName.Split('.');
  171. // string viewName = names[names.Length - 1];
  172. string name = GetName(typeof(T).FullName);
  173. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  174. {
  175. return false;
  176. }
  177. if (hideOthers)
  178. {
  179. HideAllView(name);
  180. }
  181. UIView obj = null;
  182. if (_viewDic.ContainsKey(name))
  183. {
  184. obj = _viewDic[name];
  185. }
  186. else
  187. {
  188. obj = new T() as UIView;
  189. obj.viewName = name;
  190. _viewDic.Add(name, obj);
  191. }
  192. if (obj != null)
  193. {
  194. IUIView view = (IUIView)obj;
  195. view.viewData = viewData;
  196. if (!view.isShowing)
  197. {
  198. view.Show();
  199. }
  200. else
  201. {
  202. view.Refresh();
  203. }
  204. if (goBackParams != null)
  205. {
  206. if (!_goBackDatas.ContainsKey(name))
  207. {
  208. _goBackDatas.Add(name, new List<object[]>());
  209. }
  210. _goBackDatas[name].Add(goBackParams);
  211. }
  212. else if (resetGobackParams)
  213. {
  214. if (_goBackDatas.ContainsKey(name) == true)
  215. {
  216. _goBackDatas.Remove(name);
  217. }
  218. }
  219. Debug.Log("当前打开:" + name);
  220. }
  221. return true;
  222. }
  223. public static void Hide(string fullViewName)
  224. {
  225. string name = GetName(fullViewName);
  226. if (!_viewDic.ContainsKey(name))
  227. {
  228. return;
  229. }
  230. UIView obj = _viewDic[name];
  231. if (obj != null && obj.isShowing)
  232. {
  233. IUIView view = (IUIView)obj;
  234. view.Hide();
  235. }
  236. }
  237. public static void Hide<T>()
  238. {
  239. string name = GetName(typeof(T).FullName);
  240. if (!_viewDic.ContainsKey(name))
  241. {
  242. return;
  243. }
  244. object obj = _viewDic[name];
  245. if (obj != null)
  246. {
  247. IUIView view = (IUIView)obj;
  248. view.Hide();
  249. }
  250. }
  251. public static void GoBackFrom(string fullViewName)
  252. {
  253. string name = GetName(fullViewName);
  254. ViewManager.Hide(name);
  255. if (_goBackDatas.ContainsKey(name) && _goBackDatas[name].Count > 0)
  256. {
  257. List<object[]> goBackdatas = _goBackDatas[name];
  258. object[] gobackItems = goBackdatas[goBackdatas.Count - 1];
  259. string tViewName = gobackItems[0] as string;
  260. object tViewData = null;
  261. if (gobackItems.Length > 1)
  262. {
  263. tViewData = gobackItems[1];
  264. }
  265. ViewManager.Show(tViewName, tViewData);
  266. _goBackDatas[name].RemoveAt(goBackdatas.Count - 1);
  267. }
  268. else
  269. {
  270. ViewManager.Show(ViewName.MAINUI_VIEW, null, null, true);
  271. }
  272. }
  273. public static object[] GetGoBackDatas(string fullViewName)
  274. {
  275. string name = GetName(fullViewName);
  276. object[] value = null;
  277. if (_goBackDatas.ContainsKey(name) && _goBackDatas[name].Count > 0)
  278. {
  279. value = _goBackDatas[name][_goBackDatas[name].Count - 1];
  280. }
  281. return value;
  282. }
  283. public static UIView GetUIView(string viewName)
  284. {
  285. UIView obj = _viewDic[viewName];
  286. if (obj != null && obj.isShowing)
  287. {
  288. return obj;
  289. }
  290. return null;
  291. }
  292. public static void ClearUIView(string viewName)
  293. {
  294. if (!string.IsNullOrEmpty(viewName) && _viewDic.ContainsKey(viewName))
  295. {
  296. if (_viewDic[viewName] != null && !_viewDic[viewName].isShowing)
  297. {
  298. // _viewDic[viewName] = null;
  299. _viewDic.Remove(viewName);
  300. }
  301. }
  302. }
  303. public static void HideAllView(string excludeViewName = null)
  304. {
  305. for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
  306. {
  307. int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
  308. KeyValuePair<string, UIView> kv = _viewDic.ElementAt(index);
  309. if (kv.Key != excludeViewName)
  310. {
  311. if (kv.Key == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  312. Hide(kv.Key);
  313. }
  314. }
  315. // _viewDic.Clear();
  316. // foreach (string viewName in _viewDic.Keys)
  317. // {
  318. // if (viewName != excludeViewName)
  319. // {
  320. // if (viewName == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  321. // Hide(viewName);
  322. // }
  323. // }
  324. }
  325. public static void CheckDispsoe()
  326. {
  327. for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
  328. {
  329. int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
  330. KeyValuePair<string, UIView> kv = _viewDic.ElementAt(index);
  331. if (kv.Value.isShowing == true) continue;
  332. // if (kv.Value.packageName == ResPathUtil.GetUIPackagePath("CommonGame") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Common") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
  333. long currentTime = TimeHelper.ClientNowSeconds();
  334. long closeTime = kv.Value.closeTime;
  335. if (closeTime > 0 && currentTime - closeTime >= TimeUtil.SECOND_PER_MUNITE * 1)
  336. {
  337. kv.Value.closeTime = 0;
  338. kv.Value.Dispose();
  339. }
  340. }
  341. }
  342. private static object CreateViewInstance(string name)
  343. {
  344. // Debug.LogFormat("CreateViewInstance {0}", name);
  345. Type type = Type.GetType(name);
  346. if (type != null)
  347. {
  348. return Activator.CreateInstance(type);
  349. }
  350. return null;
  351. }
  352. private static GComponent CreateLayer(string name)
  353. {
  354. GComponent layer = new GComponent();
  355. layer.name = name;
  356. GRoot.inst.AddChild(layer);
  357. layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
  358. layer.AddRelation(GRoot.inst, RelationType.Size);
  359. return layer;
  360. }
  361. public static bool CheckIsTopView(GComponent viewCom)
  362. {
  363. if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
  364. if (viewCom.parent != null)
  365. {
  366. int index = viewCom.parent.GetChildIndex(viewCom);
  367. if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
  368. {
  369. return true;
  370. }
  371. }
  372. if (GRoot.inst.GetTopWindow() == viewCom)
  373. {
  374. return true;
  375. }
  376. return false;
  377. }
  378. public static string GetName(string fullName)
  379. {
  380. string[] names = fullName.Split('.');
  381. string name = names[names.Length - 1];
  382. return name;
  383. }
  384. public static void SetMaskAlpha(float alpha)
  385. {
  386. GRoot.inst.modalLayer.alpha = alpha;
  387. }
  388. }
  389. }