ViewManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4. using FairyGUI;
  5. namespace GFGGame
  6. {
  7. /// <summary>
  8. /// ��ͼ������
  9. /// ������ͼ����ʾ������
  10. /// </summary>
  11. public class ViewManager
  12. {
  13. private static Dictionary<string, UIView> _viewDic;
  14. private static GComponent _bottomLayer;
  15. private static GComponent _topLayer;
  16. private static GComponent _guideLayer;
  17. private static GComponent _modalLayer;
  18. private static GComponent _alertLayer;
  19. private static GComponent _debugLayer;
  20. private static GComponent _floatLayer;
  21. private static Dictionary<string, object[]> _goBackDatas = new Dictionary<string, object[]>();
  22. public static void Init()
  23. {
  24. GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("Common"));
  25. UIConfig.buttonSound = (NAudioClip)UIPackage.GetItemAsset("Common", "click");
  26. Font afont = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("STZHONGS"));
  27. FontManager.RegisterFont(new DynamicFont("STZhongsong", afont));
  28. UIConfig.defaultFont = "STZhongsong";
  29. Font afont1 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("ZIHUN125"));
  30. FontManager.RegisterFont(new DynamicFont("ZIHUN125", afont1));
  31. Font afont2 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SIMKAI"));
  32. FontManager.RegisterFont(new DynamicFont("SIMKAI", afont2));
  33. Font afont3 = GFGAsset.Load<Font>(ResPathUtil.GetFontPath("SHUANGYUJUTI"));
  34. FontManager.RegisterFont(new DynamicFont("SHUANGYUJUTI", afont3));
  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 viewName, object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false)
  89. {
  90. string name = GetName(viewName);
  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(viewName) as UIView;
  107. _viewDic.Add(name, obj);
  108. }
  109. if (obj != null)
  110. {
  111. IUIView view = (IUIView)obj;
  112. view.viewData = viewData;
  113. if (!view.isShowing)
  114. {
  115. view.Show();
  116. }
  117. else
  118. {
  119. view.Refresh();
  120. }
  121. if (resetGobackParams)
  122. {
  123. if (_goBackDatas.ContainsKey(name) == true)
  124. {
  125. _goBackDatas.Remove(name);
  126. }
  127. }
  128. if (goBackParams != null)
  129. {
  130. _goBackDatas[name] = goBackParams;
  131. }
  132. Debug.Log("当前打开" + name);
  133. }
  134. return true;
  135. }
  136. public static bool isViewOpen(string viewName)
  137. {
  138. string name = GetName(viewName);
  139. UIView obj = null;
  140. if (_viewDic.ContainsKey(name))
  141. {
  142. obj = _viewDic[name];
  143. if (obj != null)
  144. {
  145. IUIView view = (IUIView)obj;
  146. if (view.isShowing) return true;
  147. }
  148. }
  149. return false;
  150. }
  151. public static bool Show<T>(object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false) where T : class, new()
  152. {
  153. // string[] names = typeof(T).FullName.Split('.');
  154. // string viewName = names[names.Length - 1];
  155. string name = GetName(typeof(T).FullName);
  156. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  157. {
  158. return false;
  159. }
  160. if (hideOthers)
  161. {
  162. HideAllView(name);
  163. }
  164. UIView obj = null;
  165. if (_viewDic.ContainsKey(name))
  166. {
  167. obj = _viewDic[name];
  168. }
  169. else
  170. {
  171. obj = new T() as UIView;
  172. _viewDic.Add(name, obj);
  173. }
  174. if (obj != null)
  175. {
  176. IUIView view = (IUIView)obj;
  177. view.viewData = viewData;
  178. if (!view.isShowing)
  179. {
  180. view.Show();
  181. }
  182. else
  183. {
  184. view.Refresh();
  185. }
  186. if (goBackParams != null)
  187. {
  188. _goBackDatas[name] = goBackParams;
  189. }
  190. else if (resetGobackParams)
  191. {
  192. if (_goBackDatas.ContainsKey(name) == true)
  193. {
  194. _goBackDatas.Remove(name);
  195. }
  196. }
  197. Debug.Log("当前打开:" + name);
  198. }
  199. return true;
  200. }
  201. public static void Hide(string viewName)
  202. {
  203. string name = GetName(viewName);
  204. if (!_viewDic.ContainsKey(name))
  205. {
  206. return;
  207. }
  208. UIView obj = _viewDic[name];
  209. if (obj != null && obj.isShowing)
  210. {
  211. IUIView view = (IUIView)obj;
  212. view.Hide();
  213. }
  214. }
  215. public static void Hide<T>()
  216. {
  217. string name = GetName(typeof(T).FullName);
  218. if (!_viewDic.ContainsKey(name))
  219. {
  220. return;
  221. }
  222. object obj = _viewDic[name];
  223. if (obj != null)
  224. {
  225. IUIView view = (IUIView)obj;
  226. view.Hide();
  227. }
  228. }
  229. public static void GoBackFrom(string viewName, bool clearGoBackDatas = false)
  230. {
  231. string name = GetName(viewName);
  232. ViewManager.Hide(name);
  233. if (_goBackDatas.ContainsKey(name))
  234. {
  235. object[] gobackItems = _goBackDatas[name];
  236. string tViewName = gobackItems[0] as string;
  237. object tViewData = null;
  238. if (gobackItems.Length > 1)
  239. {
  240. tViewData = gobackItems[1];
  241. }
  242. ViewManager.Show(tViewName, tViewData);
  243. }
  244. else
  245. {
  246. ViewManager.Show(ViewName.MAINUI_VIEW, null, null, true);
  247. }
  248. }
  249. public static object[] GetGoBackDatas(string viewName)
  250. {
  251. string name = GetName(viewName);
  252. object[] value;
  253. _goBackDatas.TryGetValue(name, out value);
  254. return value;
  255. }
  256. public static UIView GetUIView(string viewName)
  257. {
  258. UIView obj = _viewDic[viewName];
  259. if (obj != null && obj.isShowing)
  260. {
  261. return obj;
  262. }
  263. return null;
  264. }
  265. public static void HideAllView(string excludeViewName = null)
  266. {
  267. foreach (string viewName in _viewDic.Keys)
  268. {
  269. if (viewName != excludeViewName)
  270. {
  271. if (viewName == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  272. Hide(viewName);
  273. }
  274. }
  275. }
  276. private static object CreateViewInstance(string name)
  277. {
  278. // Debug.LogFormat("CreateViewInstance {0}", name);
  279. Type type = Type.GetType(name);
  280. if (type != null)
  281. {
  282. return Activator.CreateInstance(type);
  283. }
  284. return null;
  285. }
  286. private static GComponent CreateLayer(string name)
  287. {
  288. GComponent layer = new GComponent();
  289. layer.name = name;
  290. GRoot.inst.AddChild(layer);
  291. layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
  292. layer.AddRelation(GRoot.inst, RelationType.Size);
  293. return layer;
  294. }
  295. public static bool CheckIsTopView(GComponent viewCom)
  296. {
  297. if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
  298. if (viewCom.parent != null)
  299. {
  300. int index = viewCom.parent.GetChildIndex(viewCom);
  301. if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
  302. {
  303. return true;
  304. }
  305. }
  306. if (GRoot.inst.GetTopWindow() == viewCom)
  307. {
  308. return true;
  309. }
  310. return false;
  311. }
  312. public static string GetName(string fullName)
  313. {
  314. string[] names = fullName.Split('.');
  315. string name = names[names.Length - 1];
  316. return name;
  317. }
  318. public static void SetMaskAlpha(float alpha)
  319. {
  320. GRoot.inst.modalLayer.alpha = alpha;
  321. }
  322. }
  323. }