UIView.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using ET;
  2. using FairyGUI;
  3. using System.Collections;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. /// <summary>
  8. /// 视图基类,界面不可以直接继承
  9. /// </summary>
  10. public class UIView : IUIView
  11. {
  12. private string _descFilePath;
  13. private EnumViewAnimationType _viewAnimationType = EnumViewAnimationType.None;
  14. /// <summary>
  15. /// 打开视图传递的参数
  16. /// </summary>
  17. public object viewData { get; set; }
  18. public string viewName { get; set; }
  19. /// <summary>
  20. /// 设置视图组件
  21. /// </summary>
  22. public virtual GComponent viewCom { get; set; }
  23. /// <summary>
  24. /// 设置是否全屏自适应
  25. /// </summary>
  26. public bool isfullScreen { get; set; }
  27. /// <summary>
  28. /// 设置是否可以返回界面
  29. /// </summary>
  30. public bool isReturnView { get; set; }
  31. /// <summary>
  32. /// 设置是否可以返回窗口
  33. /// </summary>
  34. public bool isReturnWindow { get; set; }
  35. /// <summary>
  36. /// 设置返回界面是否刷新
  37. /// </summary>
  38. public bool backRefresh { get; set; }
  39. /// <summary>
  40. /// FairyGUI包名
  41. /// </summary>
  42. public string packageName
  43. {
  44. get
  45. {
  46. return _descFilePath;
  47. }
  48. set
  49. {
  50. _descFilePath = ResPathUtil.GetUIPackagePath(value);
  51. GFGUIPackage.AddPackage(_descFilePath);
  52. }
  53. }
  54. /// <summary>
  55. /// 界面关闭时间
  56. /// </summary>
  57. /// <value></value>
  58. public long closeTime { get; set; }
  59. /// <summary>
  60. /// 是否显示中
  61. /// </summary>
  62. public bool isShowing
  63. {
  64. get { return viewCom != null && viewCom.onStage; }
  65. }
  66. /// <summary>
  67. /// 设置视图显示和隐藏的动画类型
  68. /// </summary>
  69. protected EnumViewAnimationType viewAnimationType
  70. {
  71. set
  72. {
  73. _viewAnimationType = value;
  74. }
  75. }
  76. public UIView()
  77. {
  78. //
  79. Init();
  80. OnInit();
  81. if (viewCom != null)
  82. {
  83. viewCom.onAddedToStage.Add(__addedToStage);
  84. viewCom.onRemovedFromStage.Add(__removeFromStage);
  85. }
  86. }
  87. public virtual void Dispose()
  88. {
  89. if (viewCom != null)
  90. {
  91. viewCom.RemoveFromParent();
  92. viewCom.Dispose();
  93. viewCom = null;
  94. }
  95. if (_descFilePath != null)
  96. {
  97. if (packageName != ResPathUtil.GetUIPackagePath("CommonGame") && packageName != ResPathUtil.GetUIPackagePath("Common") && packageName != ResPathUtil.GetUIPackagePath("Main"))
  98. {
  99. GFGUIPackage.ReleasePackage(_descFilePath);
  100. } //这几个包不释放
  101. }
  102. _descFilePath = null;
  103. viewData = null;
  104. ViewManager.ClearUIView(viewName);
  105. }
  106. virtual protected void Init()
  107. {
  108. }
  109. virtual protected void OnInit()
  110. {
  111. }
  112. /// <summary>
  113. /// 界面打开状态刷新界面
  114. /// </summary>
  115. virtual public void Refresh()
  116. {
  117. object temp = viewData;
  118. this.OnHide();
  119. this.viewData = temp;
  120. this.BringToFront();
  121. this.OnShown();
  122. }
  123. public virtual void Show()
  124. {
  125. }
  126. virtual protected void BringToFront()
  127. {
  128. }
  129. public virtual void Hide()
  130. {
  131. // UIPackageManager.Instance.RemovePackageView(_descFilePath, viewName);
  132. // UIPackageManager.Instance.AddCloseTime(_descFilePath);
  133. closeTime = TimeHelper.ClientNowSeconds();
  134. DoHideAnimation();
  135. ViewManager.HideWin(this.viewName);
  136. EventAgent.DispatchEvent(ConstMessage.VIEW_CLOSED);
  137. }
  138. virtual protected void OnShown()
  139. {
  140. if (isfullScreen)
  141. {
  142. MakeFullScreen(viewCom);
  143. }
  144. AddEventListener();
  145. // if (GuideController.IsGuide())
  146. // {
  147. // Timers.inst.AddUpdate(UpdateToCheckGuide);
  148. // }
  149. }
  150. virtual protected void OnHide()
  151. {
  152. viewData = null;
  153. RemoveEventListener();
  154. // Timers.inst.Remove(UpdateToCheckGuide);
  155. TryCompleteGuide();
  156. }
  157. virtual protected void RemoveEventListener()
  158. {
  159. }
  160. virtual protected void AddEventListener()
  161. {
  162. }
  163. virtual protected void UpdateToCheckGuide(object param)
  164. {
  165. }
  166. virtual protected void TryCompleteGuide()
  167. {
  168. }
  169. protected virtual void DoShowAnimation()
  170. {
  171. OnShown();
  172. switch (_viewAnimationType)
  173. {
  174. case EnumViewAnimationType.ZOOM_CENTER:
  175. ViewAnimationFactory.ZoomInCenter(this.viewCom);
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. protected virtual void DoHideAnimation()
  182. {
  183. switch (_viewAnimationType)
  184. {
  185. case EnumViewAnimationType.ZOOM_CENTER:
  186. ViewAnimationFactory.ZoomOutCenter(this.viewCom, this.OnDoHideAnimationCompleted);
  187. break;
  188. default:
  189. this.OnDoHideAnimationCompleted();
  190. break;
  191. }
  192. }
  193. protected virtual void OnDoHideAnimationCompleted()
  194. {
  195. if (this.viewCom != null)
  196. {
  197. this.viewCom.RemoveFromParent();
  198. }
  199. }
  200. private void MakeFullScreen(GObject ui)
  201. {
  202. ui.MakeFullScreen();
  203. ui.AddRelation(GRoot.inst, RelationType.Size);
  204. }
  205. void __addedToStage()
  206. {
  207. DoShowAnimation();
  208. }
  209. void __removeFromStage()
  210. {
  211. OnHide();
  212. if (_descFilePath == ResPathUtil.GetUIPackagePath("Login") || _descFilePath == ResPathUtil.GetUIPackagePath("Loading") || _descFilePath == ResPathUtil.GetUIPackagePath("CreateRole"))//这几个界面关闭后立即释放
  213. {
  214. Dispose();
  215. }
  216. }
  217. }
  218. }