ViewManager.cs 8.7 KB

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