Files
Cielonos/Assets/Scripts/SLSUtilities/UI/UIPageBase.cs

75 lines
2.2 KiB
C#
Raw Normal View History

2026-05-10 11:47:55 -04:00
using System;
using Sirenix.OdinInspector;
using UnityEngine;
namespace SLSUtilities.UI
{
/// <summary>
/// 覆盖式 UI 页面基类(机械台、物流中心、地图、结算等)。
/// 继承自 UIElementBase额外提供 Open/Close 生命周期和 UIPageManager 注册。
/// 页面打开时自动阻塞游戏输入,关闭时恢复。
/// </summary>
[RequireComponent(typeof(CanvasGroup))]
public class UIPageBase : UIElementBase
{
/// <summary>当前页面是否处于打开状态。</summary>
2026-06-12 17:11:39 -04:00
[ShowInInspector]
[ReadOnly]
public bool IsOpen { get; protected set; }
2026-05-10 11:47:55 -04:00
/// <summary>是否允许按 ESC 关闭此页面。</summary>
2026-06-05 04:21:00 -04:00
public virtual bool CloseOnEsc => true;
2026-05-10 11:47:55 -04:00
/// <summary>页面打开后触发。</summary>
public event Action PageOpened;
/// <summary>页面关闭后触发。</summary>
public event Action PageClosed;
protected virtual void Start()
{
Hide();
}
/// <summary>
/// 打开页面:显示 UI注册到 UIPageManager 栈,触发生命周期回调。
/// 若已打开则忽略。
/// </summary>
2026-06-02 12:55:39 -04:00
[Button]
2026-05-10 11:47:55 -04:00
public virtual void Open()
{
if (IsOpen) return;
IsOpen = true;
Show();
UIPageManager.Instance.RegisterPage(this);
OnPageOpened();
}
/// <summary>
/// 关闭页面:从 UIPageManager 栈中移除,隐藏 UI触发生命周期回调。
/// 若未打开则忽略。
/// </summary>
2026-06-02 12:55:39 -04:00
[Button]
2026-05-10 11:47:55 -04:00
public virtual void Close()
{
if (!IsOpen) return;
IsOpen = false;
UIPageManager.Instance.UnregisterPage(this);
Hide();
OnPageClosed();
}
/// <summary>页面打开后的可覆盖回调。</summary>
2026-06-12 17:11:39 -04:00
protected virtual void OnPageOpened()
{
PageOpened?.Invoke();
}
2026-05-10 11:47:55 -04:00
/// <summary>页面关闭后的可覆盖回调。</summary>
2026-06-12 17:11:39 -04:00
protected virtual void OnPageClosed()
{
PageClosed?.Invoke();
}
2026-05-10 11:47:55 -04:00
}
}