ViewManager.cs 7.3 KB

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