ViewManager.cs 14 KB

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