UIPanel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /****************************************************************************
  2. * Copyright (c) 2017 xiaojun
  3. * Copyright (c) 2017 liangxie
  4. * Copyright (c) 2017 imagicbell
  5. * Copyright (c) 2018.5 ~ 2021.1 liangxie
  6. *
  7. * http://qframework.io
  8. * https://github.com/liangxiegame/QFramework
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. ****************************************************************************/
  28. namespace QFramework
  29. {
  30. using UnityEngine;
  31. /// <summary>
  32. /// 每个UIbehaviour对应的Data
  33. /// </summary>
  34. public interface IUIData
  35. {
  36. }
  37. public class UIPanelData : IUIData
  38. {
  39. protected UIPanel mPanel;
  40. }
  41. public abstract partial class UIPanel : QMonoBehaviour, IPanel
  42. {
  43. public Transform Transform
  44. {
  45. get { return transform; }
  46. }
  47. IPanelLoader IPanel.Loader { get; set; }
  48. public PanelInfo Info { get; set; }
  49. public PanelState State { get; set; }
  50. protected IUIData mUIData;
  51. public override IManager Manager
  52. {
  53. get { return UIManager.Instance; }
  54. }
  55. protected override void OnBeforeDestroy()
  56. {
  57. ClearUIComponents();
  58. }
  59. protected virtual void ClearUIComponents()
  60. {
  61. }
  62. public void Init(IUIData uiData = null)
  63. {
  64. mUIData = uiData;
  65. OnInit(uiData);
  66. }
  67. public void Open(IUIData uiData = null)
  68. {
  69. State = PanelState.Opening;
  70. OnOpen(uiData);
  71. }
  72. public override void Hide()
  73. {
  74. State = PanelState.Hide;
  75. base.Hide();
  76. }
  77. protected virtual void OnInit(IUIData uiData = null)
  78. {
  79. }
  80. protected virtual void OnOpen(IUIData uiData = null)
  81. {
  82. }
  83. /// <summary>
  84. /// avoid override in child class
  85. /// </summary>
  86. protected sealed override void OnDestroy()
  87. {
  88. base.OnDestroy();
  89. }
  90. /// <summary>
  91. /// 关闭,不允许子类调用
  92. /// </summary>
  93. void IPanel.Close(bool destroyed)
  94. {
  95. Info.UIData = mUIData;
  96. mOnClosed?.Invoke();
  97. mOnClosed = null;
  98. Hide();
  99. State = PanelState.Closed;
  100. OnClose();
  101. if (destroyed)
  102. {
  103. Destroy(gameObject);
  104. }
  105. var panelInterface = this as IPanel;
  106. panelInterface.Loader.Unload();
  107. UIKit.Config.PanelLoaderPool.RecycleLoader(panelInterface.Loader);
  108. panelInterface.Loader = null;
  109. mUIData = null;
  110. }
  111. protected void CloseSelf()
  112. {
  113. UIKit.ClosePanel(this);
  114. }
  115. protected void Back()
  116. {
  117. UIKit.Back(name);
  118. }
  119. /// <summary>
  120. /// 必须使用这个
  121. /// </summary>
  122. protected abstract void OnClose();
  123. private System.Action mOnClosed;
  124. public void OnClosed(System.Action onPanelClosed)
  125. {
  126. mOnClosed = onPanelClosed;
  127. }
  128. }
  129. }