UIView.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. if (GuideController.IsGuide())
  109. {
  110. Timers.inst.AddUpdate(UpdateToCheckGuide);
  111. }
  112. }
  113. virtual protected void OnHide()
  114. {
  115. viewData = null;
  116. Timers.inst.Remove(UpdateToCheckGuide);
  117. TryCompleteGuide();
  118. }
  119. virtual protected void UpdateToCheckGuide(object param)
  120. {
  121. }
  122. virtual protected void TryCompleteGuide()
  123. {
  124. }
  125. protected virtual void DoShowAnimation()
  126. {
  127. OnShown();
  128. switch (_viewAnimationType)
  129. {
  130. case EnumViewAnimationType.ZOOM_CENTER:
  131. ViewAnimationFactory.ZoomInCenter(this.viewCom);
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. protected virtual void DoHideAnimation()
  138. {
  139. switch (_viewAnimationType)
  140. {
  141. case EnumViewAnimationType.ZOOM_CENTER:
  142. ViewAnimationFactory.ZoomOutCenter(this.viewCom, this.OnDoHideAnimationCompleted);
  143. break;
  144. default:
  145. this.OnDoHideAnimationCompleted();
  146. break;
  147. }
  148. }
  149. protected virtual void OnDoHideAnimationCompleted()
  150. {
  151. if (this.viewCom != null)
  152. {
  153. this.viewCom.RemoveFromParent();
  154. }
  155. }
  156. private void MakeFullScreen(GObject ui)
  157. {
  158. ui.MakeFullScreen();
  159. ui.AddRelation(GRoot.inst, RelationType.Size);
  160. }
  161. void __addedToStage()
  162. {
  163. DoShowAnimation();
  164. }
  165. void __removeFromStage()
  166. {
  167. OnHide();
  168. }
  169. }
  170. }