UIView.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.OnShown();
  113. }
  114. public virtual void Show()
  115. {
  116. }
  117. public virtual void Hide()
  118. {
  119. // UIPackageManager.Instance.RemovePackageView(_descFilePath, viewName);
  120. // UIPackageManager.Instance.AddCloseTime(_descFilePath);
  121. closeTime = TimeHelper.ClientNowSeconds();
  122. DoHideAnimation();
  123. ViewManager.HideWin(this.viewName);
  124. EventAgent.DispatchEvent(ConstMessage.VIEW_CLOSED);
  125. }
  126. virtual protected void OnShown()
  127. {
  128. if (isfullScreen)
  129. {
  130. MakeFullScreen(viewCom);
  131. }
  132. AddEventListener();
  133. // if (GuideController.IsGuide())
  134. // {
  135. // Timers.inst.AddUpdate(UpdateToCheckGuide);
  136. // }
  137. }
  138. virtual protected void OnHide()
  139. {
  140. viewData = null;
  141. RemoveEventListener();
  142. // Timers.inst.Remove(UpdateToCheckGuide);
  143. TryCompleteGuide();
  144. }
  145. virtual protected void RemoveEventListener()
  146. {
  147. }
  148. virtual protected void AddEventListener()
  149. {
  150. }
  151. virtual protected void UpdateToCheckGuide(object param)
  152. {
  153. }
  154. virtual protected void TryCompleteGuide()
  155. {
  156. }
  157. protected virtual void DoShowAnimation()
  158. {
  159. OnShown();
  160. switch (_viewAnimationType)
  161. {
  162. case EnumViewAnimationType.ZOOM_CENTER:
  163. ViewAnimationFactory.ZoomInCenter(this.viewCom);
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. protected virtual void DoHideAnimation()
  170. {
  171. switch (_viewAnimationType)
  172. {
  173. case EnumViewAnimationType.ZOOM_CENTER:
  174. ViewAnimationFactory.ZoomOutCenter(this.viewCom, this.OnDoHideAnimationCompleted);
  175. break;
  176. default:
  177. this.OnDoHideAnimationCompleted();
  178. break;
  179. }
  180. }
  181. protected virtual void OnDoHideAnimationCompleted()
  182. {
  183. if (this.viewCom != null)
  184. {
  185. this.viewCom.RemoveFromParent();
  186. }
  187. }
  188. private void MakeFullScreen(GObject ui)
  189. {
  190. ui.MakeFullScreen();
  191. ui.AddRelation(GRoot.inst, RelationType.Size);
  192. }
  193. void __addedToStage()
  194. {
  195. DoShowAnimation();
  196. }
  197. void __removeFromStage()
  198. {
  199. OnHide();
  200. if (_descFilePath == ResPathUtil.GetUIPackagePath("Login") || _descFilePath == ResPathUtil.GetUIPackagePath("Loading") || _descFilePath == ResPathUtil.GetUIPackagePath("CreateRole"))//这几个界面关闭后立即释放
  201. {
  202. Dispose();
  203. }
  204. }
  205. }
  206. }