ViewManager.cs 7.0 KB

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