ViewManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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">要传递给视图的参数</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.CheckIsFunOpenBgViewName(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 Show<T>(object viewData = null, object[] goBackParams = null, bool hideOthers = false, bool resetGobackParams = false) where T : class, new()
  95. {
  96. string viewName = typeof(T).FullName;
  97. if (!FunctionOpenDataManager.Instance.CheckIsFunOpenBgViewName(viewName))
  98. {
  99. return false;
  100. }
  101. if (hideOthers)
  102. {
  103. HideAllView(viewName);
  104. }
  105. UIView obj = null;
  106. if (_viewDic.ContainsKey(viewName))
  107. {
  108. obj = _viewDic[viewName];
  109. }
  110. else
  111. {
  112. obj = new T() as UIView;
  113. _viewDic.Add(viewName, obj);
  114. }
  115. if (obj != null)
  116. {
  117. IUIView view = (IUIView)obj;
  118. view.viewData = viewData;
  119. if (!view.isShowing)
  120. {
  121. view.Show();
  122. Debug.Log("当前打开:" + viewName);
  123. }
  124. else
  125. {
  126. view.Refresh();
  127. }
  128. if (goBackParams != null)
  129. {
  130. _goBackDatas[viewName] = goBackParams;
  131. }
  132. else if (resetGobackParams)
  133. {
  134. if (_goBackDatas.ContainsKey(viewName) == true)
  135. {
  136. _goBackDatas.Remove(viewName);
  137. }
  138. }
  139. }
  140. return true;
  141. }
  142. public static void Hide(string viewName)
  143. {
  144. if (!_viewDic.ContainsKey(viewName))
  145. {
  146. return;
  147. }
  148. UIView obj = _viewDic[viewName];
  149. if (obj != null && obj.isShowing)
  150. {
  151. IUIView view = (IUIView)obj;
  152. view.Hide();
  153. }
  154. }
  155. public static void Hide<T>()
  156. {
  157. string viewName = typeof(T).FullName;
  158. if (!_viewDic.ContainsKey(viewName))
  159. {
  160. return;
  161. }
  162. object obj = _viewDic[viewName];
  163. if (obj != null)
  164. {
  165. IUIView view = (IUIView)obj;
  166. view.Hide();
  167. }
  168. }
  169. public static void GoBackFrom(string viewName, bool clearGoBackDatas = false)
  170. {
  171. ViewManager.Hide(viewName);
  172. if (_goBackDatas.ContainsKey(viewName))
  173. {
  174. object[] gobackItems = _goBackDatas[viewName];
  175. string tViewName = gobackItems[0] as string;
  176. object tViewData = null;
  177. if (gobackItems.Length > 1)
  178. {
  179. tViewData = gobackItems[1];
  180. }
  181. ViewManager.Show(tViewName, tViewData);
  182. }
  183. }
  184. public static object[] GetGoBackDatas(string viewName)
  185. {
  186. object[] value;
  187. _goBackDatas.TryGetValue(viewName, out value);
  188. return value;
  189. }
  190. public static void HideAllView(string excludeViewName = null)
  191. {
  192. foreach (string viewName in _viewDic.Keys)
  193. {
  194. if (viewName != excludeViewName)
  195. {
  196. Hide(viewName);
  197. }
  198. }
  199. }
  200. private static object CreateViewInstance(string name)
  201. {
  202. // Debug.LogFormat("CreateViewInstance {0}", name);
  203. Type type = Type.GetType(name);
  204. if (type != null)
  205. {
  206. return Activator.CreateInstance(type);
  207. }
  208. return null;
  209. }
  210. private static GComponent CreateLayer(string name)
  211. {
  212. GComponent layer = new GComponent();
  213. layer.name = name;
  214. GRoot.inst.AddChild(layer);
  215. layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
  216. layer.AddRelation(GRoot.inst, RelationType.Size);
  217. return layer;
  218. }
  219. }
  220. }