ViewManager.cs 9.7 KB

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