UIView.cs 6.0 KB

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