ViewManager.cs 11 KB

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