UIView.cs 5.6 KB

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