ViewManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. _viewDic = new Dictionary<string, UIView>();
  34. //��ʼ����ͼ������
  35. _bottomLayer = CreateLayer("BottomLayer");
  36. _topLayer = CreateLayer("TopLayer");
  37. _topLayer.sortingOrder = ConstSortingOrder.TOP;
  38. _guideLayer = CreateLayer("GuideLayer");
  39. _guideLayer.sortingOrder = ConstSortingOrder.Guide;
  40. _modalLayer = CreateLayer("ModalLayer");
  41. _modalLayer.sortingOrder = ConstSortingOrder.Modal;
  42. _alertLayer = CreateLayer("AlertLayer");
  43. _alertLayer.sortingOrder = ConstSortingOrder.Alert;
  44. //debug层
  45. _debugLayer = CreateLayer("DebugLayer");
  46. _debugLayer.sortingOrder = ConstSortingOrder.Debug;
  47. _floatLayer = CreateLayer("FloatLayer");
  48. _floatLayer.sortingOrder = ConstSortingOrder.Float;
  49. SetMaskAlpha(0.6f);
  50. }
  51. public static void AddChildToBottomLayer(GObject gObject)
  52. {
  53. _bottomLayer.AddChild(gObject);
  54. }
  55. public static void AddChildToTopLayer(GObject gObject)
  56. {
  57. _topLayer.AddChild(gObject);
  58. }
  59. public static void AddChildToGuideLayer(GObject gObject)
  60. {
  61. _guideLayer.AddChild(gObject);
  62. }
  63. public static void AddChildToModalLayer(GObject gObject)
  64. {
  65. _modalLayer.AddChild(gObject);
  66. }
  67. public static void AddChildToAlertLayer(GObject gObject)
  68. {
  69. _alertLayer.AddChild(gObject);
  70. }
  71. public static void AddChildToDebugLayer(GObject gObject)
  72. {
  73. _debugLayer.AddChild(gObject);
  74. }
  75. public static void AddChildToFloatLayer(GObject gObject)
  76. {
  77. _floatLayer.AddChild(gObject);
  78. }
  79. /// <summary>
  80. /// ��ʾһ����ͼ
  81. /// </summary>
  82. /// <param name="name">Ҫ��ʾ����ͼ����</param>
  83. /// <param name="viewData">Ҫ���ݸ���ͼ�IJ���</param>
  84. /// <param name="goBackParams">�Ӹ���ͼ���ص���ͼ��Ϣ</param>
  85. /// <param name="hideOthers">�Ƿ�ر�������ͼ</param>
  86. public static void Show(string viewName, object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false)
  87. {
  88. string name = GetName(viewName);
  89. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  90. {
  91. return;
  92. }
  93. if (hideOthers)
  94. {
  95. HideAllView(name);
  96. }
  97. UIView obj = null;
  98. if (_viewDic.ContainsKey(name))
  99. {
  100. obj = _viewDic[name];
  101. }
  102. else
  103. {
  104. obj = CreateViewInstance(viewName) as UIView;
  105. _viewDic.Add(name, obj);
  106. }
  107. if (obj != null)
  108. {
  109. IUIView view = (IUIView)obj;
  110. view.viewData = viewData;
  111. if (!view.isShowing)
  112. {
  113. view.Show();
  114. Debug.Log("��ǰ�򿪣�" + name);
  115. }
  116. else
  117. {
  118. view.Refresh();
  119. }
  120. if (resetGobackParams)
  121. {
  122. if (_goBackDatas.ContainsKey(name) == true)
  123. {
  124. _goBackDatas.Remove(name);
  125. }
  126. }
  127. if (goBackParams != null)
  128. {
  129. _goBackDatas[name] = goBackParams;
  130. }
  131. // else
  132. }
  133. }
  134. public static bool isViewOpen(string viewName)
  135. {
  136. string name = GetName(viewName);
  137. UIView obj = null;
  138. if (_viewDic.ContainsKey(name))
  139. {
  140. obj = _viewDic[name];
  141. if (obj != null)
  142. {
  143. IUIView view = (IUIView)obj;
  144. if (view.isShowing) return true;
  145. }
  146. }
  147. return false;
  148. }
  149. public static bool Show<T>(object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false) where T : class, new()
  150. {
  151. // string[] names = typeof(T).FullName.Split('.');
  152. // string viewName = names[names.Length - 1];
  153. string name = GetName(typeof(T).FullName);
  154. if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
  155. {
  156. return false;
  157. }
  158. if (hideOthers)
  159. {
  160. HideAllView(name);
  161. }
  162. UIView obj = null;
  163. if (_viewDic.ContainsKey(name))
  164. {
  165. obj = _viewDic[name];
  166. }
  167. else
  168. {
  169. obj = new T() as UIView;
  170. _viewDic.Add(name, obj);
  171. }
  172. if (obj != null)
  173. {
  174. IUIView view = (IUIView)obj;
  175. view.viewData = viewData;
  176. if (!view.isShowing)
  177. {
  178. view.Show();
  179. //Debug.Log("当前打开:" + name);
  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. }
  197. return true;
  198. }
  199. public static void Hide(string viewName)
  200. {
  201. string name = GetName(viewName);
  202. if (!_viewDic.ContainsKey(name))
  203. {
  204. return;
  205. }
  206. UIView obj = _viewDic[name];
  207. if (obj != null && obj.isShowing)
  208. {
  209. IUIView view = (IUIView)obj;
  210. view.Hide();
  211. }
  212. }
  213. public static void Hide<T>()
  214. {
  215. string name = GetName(typeof(T).FullName);
  216. if (!_viewDic.ContainsKey(name))
  217. {
  218. return;
  219. }
  220. object obj = _viewDic[name];
  221. if (obj != null)
  222. {
  223. IUIView view = (IUIView)obj;
  224. view.Hide();
  225. }
  226. }
  227. public static void GoBackFrom(string viewName, bool clearGoBackDatas = false)
  228. {
  229. string name = GetName(viewName);
  230. ViewManager.Hide(name);
  231. if (_goBackDatas.ContainsKey(name))
  232. {
  233. object[] gobackItems = _goBackDatas[name];
  234. string tViewName = gobackItems[0] as string;
  235. object tViewData = null;
  236. if (gobackItems.Length > 1)
  237. {
  238. tViewData = gobackItems[1];
  239. }
  240. ViewManager.Show(tViewName, tViewData);
  241. }
  242. else
  243. {
  244. ViewManager.Show(ViewName.MAINUI_VIEW, null, null, true);
  245. }
  246. }
  247. public static object[] GetGoBackDatas(string viewName)
  248. {
  249. string name = GetName(viewName);
  250. object[] value;
  251. _goBackDatas.TryGetValue(name, out value);
  252. return value;
  253. }
  254. public static UIView GetUIView(string viewName)
  255. {
  256. UIView obj = _viewDic[viewName];
  257. if (obj != null && obj.isShowing)
  258. {
  259. return obj;
  260. }
  261. return null;
  262. }
  263. public static void HideAllView(string excludeViewName = null)
  264. {
  265. foreach (string viewName in _viewDic.Keys)
  266. {
  267. if (viewName != excludeViewName)
  268. {
  269. if (viewName == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
  270. Hide(viewName);
  271. }
  272. }
  273. }
  274. private static object CreateViewInstance(string name)
  275. {
  276. // Debug.LogFormat("CreateViewInstance {0}", name);
  277. Type type = Type.GetType(name);
  278. if (type != null)
  279. {
  280. return Activator.CreateInstance(type);
  281. }
  282. return null;
  283. }
  284. private static GComponent CreateLayer(string name)
  285. {
  286. GComponent layer = new GComponent();
  287. layer.name = name;
  288. GRoot.inst.AddChild(layer);
  289. layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
  290. layer.AddRelation(GRoot.inst, RelationType.Size);
  291. return layer;
  292. }
  293. public static bool CheckIsTopView(GComponent viewCom)
  294. {
  295. if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
  296. if (viewCom.parent != null)
  297. {
  298. int index = viewCom.parent.GetChildIndex(viewCom);
  299. if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
  300. {
  301. return true;
  302. }
  303. }
  304. if (GRoot.inst.GetTopWindow() == viewCom)
  305. {
  306. return true;
  307. }
  308. return false;
  309. }
  310. public static string GetName(string fullName)
  311. {
  312. string[] names = fullName.Split('.');
  313. string name = names[names.Length - 1];
  314. return name;
  315. }
  316. public static void SetMaskAlpha(float alpha)
  317. {
  318. GRoot.inst.modalLayer.alpha = alpha;
  319. }
  320. }
  321. }