UIView.cs 4.5 KB

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