ViewManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 void 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;
  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. }
  135. public static bool isViewOpen(string viewName)
  136. {
  137. string name = GetName(viewName);
  138. UIView obj = null;
  139. if (_viewDic.ContainsKey(name))
  140. {
  141. obj = _viewDic[name];
  142. if (obj != null)
  143. {
  144. IUIView view = (IUIView)obj;
  145. if (view.isShowing) return true;
  146. }
  147. }
  148. return false;
  149. }
  150. public static bool Show<T>(object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false) where T : class, new()
  151. {
  152. // string[] names = typeof(T).FullName.Split('.');
  153. // string viewName = names[names.Length - 1];
  154. string name = GetName(typeof(T).FullName);
  155. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  156. {
  157. return false;
  158. }
  159. if (hideOthers)
  160. {
  161. HideAllView(name);
  162. }
  163. UIView obj = null;
  164. if (_viewDic.ContainsKey(name))
  165. {
  166. obj = _viewDic[name];
  167. }
  168. else
  169. {
  170. obj = new T() as UIView;
  171. _viewDic.Add(name, obj);
  172. }
  173. if (obj != null)
  174. {
  175. IUIView view = (IUIView)obj;
  176. view.viewData = viewData;
  177. if (!view.isShowing)
  178. {
  179. view.Show();
  180. }
  181. else
  182. {
  183. view.Refresh();
  184. }
  185. if (goBackParams != null)
  186. {
  187. _goBackDatas[name] = goBackParams;
  188. }
  189. else if (resetGobackParams)
  190. {
  191. if (_goBackDatas.ContainsKey(name) == true)
  192. {
  193. _goBackDatas.Remove(name);
  194. }
  195. }
  196. Debug.Log("当前打开:" + name);
  197. }
  198. return true;
  199. }
  200. public static void Hide(string viewName)
  201. {
  202. string name = GetName(viewName);
  203. if (!_viewDic.ContainsKey(name))
  204. {
  205. return;
  206. }
  207. UIView obj = _viewDic[name];
  208. if (obj != null && obj.isShowing)
  209. {
  210. IUIView view = (IUIView)obj;
  211. view.Hide();
  212. }
  213. }
  214. public static void Hide<T>()
  215. {
  216. string name = GetName(typeof(T).FullName);
  217. if (!_viewDic.ContainsKey(name))
  218. {
  219. return;
  220. }
  221. object obj = _viewDic[name];
  222. if (obj != null)
  223. {
  224. IUIView view = (IUIView)obj;
  225. view.Hide();
  226. }
  227. }
  228. public static void GoBackFrom(string viewName, bool clearGoBackDatas = false)
  229. {
  230. string name = GetName(viewName);
  231. ViewManager.Hide(name);
  232. if (_goBackDatas.ContainsKey(name))
  233. {
  234. object[] gobackItems = _goBackDatas[name];
  235. string tViewName = gobackItems[0] as string;
  236. object tViewData = null;
  237. if (gobackItems.Length > 1)
  238. {
  239. tViewData = gobackItems[1];
  240. }
  241. ViewManager.Show(tViewName, tViewData);
  242. }
  243. else
  244. {
  245. ViewManager.Show(ViewName.MAINUI_VIEW, null, null, true);
  246. }
  247. }
  248. public static object[] GetGoBackDatas(string viewName)
  249. {
  250. string name = GetName(viewName);
  251. object[] value;
  252. _goBackDatas.TryGetValue(name, out value);
  253. return value;
  254. }
  255. public static UIView GetUIView(string viewName)
  256. {
  257. UIView obj = _viewDic[viewName];
  258. if (obj != null && obj.isShowing)
  259. {
  260. return obj;
  261. }
  262. return null;
  263. }
  264. public static void HideAllView(string excludeViewName = null)
  265. {
  266. foreach (string viewName in _viewDic.Keys)
  267. {
  268. if (viewName != excludeViewName)
  269. {
  270. if (viewName == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  271. Hide(viewName);
  272. }
  273. }
  274. }
  275. private static object CreateViewInstance(string name)
  276. {
  277. // Debug.LogFormat("CreateViewInstance {0}", name);
  278. Type type = Type.GetType(name);
  279. if (type != null)
  280. {
  281. return Activator.CreateInstance(type);
  282. }
  283. return null;
  284. }
  285. private static GComponent CreateLayer(string name)
  286. {
  287. GComponent layer = new GComponent();
  288. layer.name = name;
  289. GRoot.inst.AddChild(layer);
  290. layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
  291. layer.AddRelation(GRoot.inst, RelationType.Size);
  292. return layer;
  293. }
  294. public static bool CheckIsTopView(GComponent viewCom)
  295. {
  296. if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
  297. if (viewCom.parent != null)
  298. {
  299. int index = viewCom.parent.GetChildIndex(viewCom);
  300. if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
  301. {
  302. return true;
  303. }
  304. }
  305. if (GRoot.inst.GetTopWindow() == viewCom)
  306. {
  307. return true;
  308. }
  309. return false;
  310. }
  311. public static string GetName(string fullName)
  312. {
  313. string[] names = fullName.Split('.');
  314. string name = names[names.Length - 1];
  315. return name;
  316. }
  317. public static void SetMaskAlpha(float alpha)
  318. {
  319. GRoot.inst.modalLayer.alpha = alpha;
  320. }
  321. }
  322. }