UIView.cs 4.6 KB

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