UIView.cs 6.2 KB

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