ViewManager.cs 12 KB

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