UIView.cs 6.3 KB

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