ViewManager.cs 9.0 KB

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