ViewManager.cs 8.5 KB

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