ViewManager.cs 9.5 KB

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