UIView.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. if (packageName != ResPathUtil.GetUIPackagePath("CommonGame") && packageName != ResPathUtil.GetUIPackagePath("Common") && packageName != ResPathUtil.GetUIPackagePath("Main"))
  86. {
  87. GFGUIPackage.ReleasePackage(_descFilePath);
  88. } //这几个包不释放
  89. }
  90. _descFilePath = null;
  91. viewData = null;
  92. ViewManager.ClearUIView(viewName);
  93. }
  94. virtual protected void Init()
  95. {
  96. }
  97. virtual protected void OnInit()
  98. {
  99. }
  100. /// <summary>
  101. /// 界面打开状态刷新界面
  102. /// </summary>
  103. virtual public void Refresh()
  104. {
  105. object temp = viewData;
  106. this.OnHide();
  107. this.viewData = temp;
  108. this.OnShown();
  109. }
  110. public virtual void Show()
  111. {
  112. }
  113. public virtual void Hide()
  114. {
  115. // UIPackageManager.Instance.RemovePackageView(_descFilePath, viewName);
  116. // UIPackageManager.Instance.AddCloseTime(_descFilePath);
  117. closeTime = TimeHelper.ClientNowSeconds();
  118. DoHideAnimation();
  119. }
  120. virtual protected void OnShown()
  121. {
  122. if (isfullScreen)
  123. {
  124. MakeFullScreen(viewCom);
  125. }
  126. AddEventListener();
  127. // if (GuideController.IsGuide())
  128. // {
  129. // Timers.inst.AddUpdate(UpdateToCheckGuide);
  130. // }
  131. }
  132. virtual protected void OnHide()
  133. {
  134. viewData = null;
  135. RemoveEventListener();
  136. // Timers.inst.Remove(UpdateToCheckGuide);
  137. TryCompleteGuide();
  138. }
  139. virtual protected void RemoveEventListener()
  140. {
  141. }
  142. virtual protected void AddEventListener()
  143. {
  144. }
  145. virtual protected void UpdateToCheckGuide(object param)
  146. {
  147. }
  148. virtual protected void TryCompleteGuide()
  149. {
  150. }
  151. protected virtual void DoShowAnimation()
  152. {
  153. OnShown();
  154. switch (_viewAnimationType)
  155. {
  156. case EnumViewAnimationType.ZOOM_CENTER:
  157. ViewAnimationFactory.ZoomInCenter(this.viewCom);
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. protected virtual void DoHideAnimation()
  164. {
  165. switch (_viewAnimationType)
  166. {
  167. case EnumViewAnimationType.ZOOM_CENTER:
  168. ViewAnimationFactory.ZoomOutCenter(this.viewCom, this.OnDoHideAnimationCompleted);
  169. break;
  170. default:
  171. this.OnDoHideAnimationCompleted();
  172. break;
  173. }
  174. }
  175. protected virtual void OnDoHideAnimationCompleted()
  176. {
  177. if (this.viewCom != null)
  178. {
  179. this.viewCom.RemoveFromParent();
  180. }
  181. }
  182. private void MakeFullScreen(GObject ui)
  183. {
  184. ui.MakeFullScreen();
  185. ui.AddRelation(GRoot.inst, RelationType.Size);
  186. }
  187. void __addedToStage()
  188. {
  189. DoShowAnimation();
  190. }
  191. void __removeFromStage()
  192. {
  193. OnHide();
  194. if (_descFilePath == ResPathUtil.GetUIPackagePath("Login") || _descFilePath == ResPathUtil.GetUIPackagePath("Loading") || _descFilePath == ResPathUtil.GetUIPackagePath("CreateRole"))//这几个界面关闭后立即释放
  195. {
  196. Dispose();
  197. }
  198. }
  199. }
  200. }