BaseWindow.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using FairyGUI;
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. namespace GFGGame
  5. {
  6. /// <summary>
  7. /// FairyGUI中Window的扩展类,功能界面基本都用这个
  8. /// </summary>
  9. public class BaseWindow : UIView, IUIView
  10. {
  11. private Window _window;
  12. private GGraph _btnClose;
  13. private bool _clickBlankToClose;
  14. private bool _modal;
  15. public bool modal
  16. {
  17. get => _modal;
  18. set
  19. {
  20. _modal = value;
  21. if (_window != null)
  22. {
  23. _window.modal = modal;
  24. }
  25. }
  26. }
  27. protected bool bringToFontOnClick
  28. {
  29. set
  30. {
  31. _window.bringToFontOnClick = value;
  32. }
  33. }
  34. /// <summary>
  35. /// 点击空白关闭,true关闭,false不关闭,默认值关闭
  36. /// </summary>
  37. protected bool clickBlankToClose
  38. {
  39. set
  40. {
  41. _clickBlankToClose = value;
  42. if (this.viewCom != null && this.viewCom.root != null)
  43. {
  44. if (_clickBlankToClose)
  45. {
  46. CheckCreateCloseButton();
  47. }
  48. else
  49. {
  50. CheckRemoveCloseButton();
  51. }
  52. }
  53. }
  54. }
  55. public override GComponent viewCom
  56. {
  57. get => this._window;
  58. set
  59. {
  60. contentPane = value;
  61. }
  62. }
  63. /// <summary>
  64. /// 设置视图组件
  65. /// </summary>
  66. private GComponent contentPane
  67. {
  68. set
  69. {
  70. if (_window == null)
  71. {
  72. _window = new Window();
  73. }
  74. _window.contentPane = value;
  75. _window.modal = modal;
  76. }
  77. }
  78. public override void Dispose()
  79. {
  80. // if (packageName == ResPathUtil.GetUIPackagePath("CommonGame") || packageName == ResPathUtil.GetUIPackagePath("Common") || packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
  81. base.Dispose();
  82. if (_window != null)
  83. {
  84. _window.RemoveFromParent();
  85. _window.Dispose();
  86. _window = null;
  87. }
  88. }
  89. /// <summary>
  90. /// 打开界面
  91. /// </summary>
  92. public override void Show()
  93. {
  94. if (_window != null)
  95. {
  96. _window.Show();
  97. }
  98. }
  99. protected override void OnInit()
  100. {
  101. base.OnInit();
  102. _clickBlankToClose = true;
  103. if (isfullScreen)
  104. {
  105. viewAnimationType = EnumViewAnimationType.None;
  106. }
  107. }
  108. protected override void OnShown()
  109. {
  110. base.OnShown();
  111. CheckCreateCloseButton();
  112. }
  113. protected override void OnHide()
  114. {
  115. base.OnHide();
  116. CheckRemoveCloseButton();
  117. }
  118. protected override void OnDoHideAnimationCompleted()
  119. {
  120. if (this._window != null)
  121. {
  122. this._window.Hide();
  123. }
  124. }
  125. virtual protected void closeEventHandler(EventContext context)
  126. {
  127. Hide();
  128. }
  129. private void CheckCreateCloseButton()
  130. {
  131. if (_clickBlankToClose)
  132. {
  133. _btnClose = CloseButtonPool.GetCloseButton();
  134. _window.AddChildAt(_btnClose, 0);
  135. _btnClose.onClick.Add(closeEventHandler);
  136. _btnClose.x = -(_btnClose.width - _window.width) / 2;
  137. _btnClose.y = -(_btnClose.height - _window.height) / 2;
  138. }
  139. }
  140. private void CheckRemoveCloseButton()
  141. {
  142. if (_btnClose != null)
  143. {
  144. _btnClose.onClick.Remove(closeEventHandler);
  145. _btnClose.RemoveFromParent();
  146. CloseButtonPool.PutCloseButton(_btnClose);
  147. _btnClose = null;
  148. }
  149. }
  150. private class CloseButtonPool
  151. {
  152. private static List<GGraph> _pool = new List<GGraph>();
  153. public static GGraph GetCloseButton()
  154. {
  155. GGraph btnClose = null;
  156. if (_pool.Count > 0)
  157. {
  158. int i = _pool.Count - 1;
  159. btnClose = _pool[i];
  160. _pool.RemoveAt(_pool.Count - 1);
  161. }
  162. else
  163. {
  164. btnClose = new GGraph();
  165. btnClose.DrawRect(GRoot.inst.width, GRoot.inst.height, 0, Color.red, UIConfig.modalLayerColor);
  166. btnClose.AddRelation(GRoot.inst, RelationType.Size);
  167. btnClose.alpha = 0;
  168. }
  169. return btnClose;
  170. }
  171. public static void PutCloseButton(GGraph gGraph)
  172. {
  173. if (!_pool.Contains(gGraph))
  174. {
  175. _pool.Add(gGraph);
  176. }
  177. }
  178. }
  179. }
  180. }