ViewManager.cs 10.0 KB

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