ViewManager.cs 14 KB

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