UIView.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using FairyGUI;
  2. using System.Collections;
  3. using UnityEngine;
  4. namespace GFGGame
  5. {
  6. /// <summary>
  7. /// 视图基类,界面不可以直接继承
  8. /// </summary>
  9. public class UIView : IUIView
  10. {
  11. private string _descFilePath;
  12. private EnumViewAnimationType _viewAnimationType = EnumViewAnimationType.None;
  13. /// <summary>
  14. /// 打开视图传递的参数
  15. /// </summary>
  16. public object viewData { get; set; }
  17. public string viewName { get; set; }
  18. /// <summary>
  19. /// 设置视图组件
  20. /// </summary>
  21. public virtual GComponent viewCom { get; set; }
  22. /// <summary>
  23. /// 设置是否全屏自适应
  24. /// </summary>
  25. public bool isfullScreen { get; set; }
  26. /// <summary>
  27. /// FairyGUI包名
  28. /// </summary>
  29. protected string packageName
  30. {
  31. set
  32. {
  33. _descFilePath = ResPathUtil.GetUIPackagePath(value);
  34. GFGUIPackage.AddPackage(_descFilePath);
  35. }
  36. }
  37. /// <summary>
  38. /// 是否显示中
  39. /// </summary>
  40. public bool isShowing
  41. {
  42. get { return viewCom != null && viewCom.onStage; }
  43. }
  44. /// <summary>
  45. /// 设置视图显示和隐藏的动画类型
  46. /// </summary>
  47. protected EnumViewAnimationType viewAnimationType
  48. {
  49. set
  50. {
  51. _viewAnimationType = value;
  52. }
  53. }
  54. public UIView()
  55. {
  56. //
  57. Init();
  58. OnInit();
  59. if (viewCom != null)
  60. {
  61. viewCom.onAddedToStage.Add(__addedToStage);
  62. viewCom.onRemovedFromStage.Add(__removeFromStage);
  63. }
  64. }
  65. public virtual void Dispose()
  66. {
  67. if (viewCom != null)
  68. {
  69. viewCom.RemoveFromParent();
  70. viewCom.Dispose();
  71. viewCom = null;
  72. }
  73. if (_descFilePath != null)
  74. {
  75. GFGUIPackage.RemovePackage(_descFilePath);
  76. }
  77. _descFilePath = null;
  78. viewData = null;
  79. ViewManager.ClearUIView(viewName);
  80. }
  81. virtual protected void Init()
  82. {
  83. }
  84. virtual protected void OnInit()
  85. {
  86. }
  87. /// <summary>
  88. /// 界面打开状态刷新界面
  89. /// </summary>
  90. virtual public void Refresh()
  91. {
  92. object temp = viewData;
  93. this.OnHide();
  94. this.viewData = temp;
  95. this.OnShown();
  96. }
  97. public virtual void Show()
  98. {
  99. }
  100. public virtual void Hide()
  101. {
  102. DoHideAnimation();
  103. }
  104. virtual protected void OnShown()
  105. {
  106. if (isfullScreen)
  107. {
  108. MakeFullScreen(viewCom);
  109. }
  110. AddEventListener();
  111. // if (GuideController.IsGuide())
  112. // {
  113. // Timers.inst.AddUpdate(UpdateToCheckGuide);
  114. // }
  115. }
  116. virtual protected void OnHide()
  117. {
  118. viewData = null;
  119. RemoveEventListener();
  120. // Timers.inst.Remove(UpdateToCheckGuide);
  121. TryCompleteGuide();
  122. }
  123. virtual protected void RemoveEventListener()
  124. {
  125. }
  126. virtual protected void AddEventListener()
  127. {
  128. }
  129. virtual protected void UpdateToCheckGuide(object param)
  130. {
  131. }
  132. virtual protected void TryCompleteGuide()
  133. {
  134. }
  135. protected virtual void DoShowAnimation()
  136. {
  137. OnShown();
  138. switch (_viewAnimationType)
  139. {
  140. case EnumViewAnimationType.ZOOM_CENTER:
  141. ViewAnimationFactory.ZoomInCenter(this.viewCom);
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. protected virtual void DoHideAnimation()
  148. {
  149. switch (_viewAnimationType)
  150. {
  151. case EnumViewAnimationType.ZOOM_CENTER:
  152. ViewAnimationFactory.ZoomOutCenter(this.viewCom, this.OnDoHideAnimationCompleted);
  153. break;
  154. default:
  155. this.OnDoHideAnimationCompleted();
  156. break;
  157. }
  158. }
  159. protected virtual void OnDoHideAnimationCompleted()
  160. {
  161. if (this.viewCom != null)
  162. {
  163. this.viewCom.RemoveFromParent();
  164. }
  165. }
  166. private void MakeFullScreen(GObject ui)
  167. {
  168. ui.MakeFullScreen();
  169. ui.AddRelation(GRoot.inst, RelationType.Size);
  170. }
  171. void __addedToStage()
  172. {
  173. DoShowAnimation();
  174. }
  175. void __removeFromStage()
  176. {
  177. OnHide();
  178. Dispose();
  179. }
  180. }
  181. }