UIView.cs 4.8 KB

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