81 lines
4.7 KiB
C#
81 lines
4.7 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using PhxhSDK;
|
||
|
||
/*
|
||
╔═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
|
||
║ UIWindow 默认Close按钮处理模块 (Default Close Button) ║
|
||
╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
|
||
║ ║
|
||
║ 【功能说明】 ║
|
||
║ 处理UIWindow的默认"关闭"按钮的自动绑定和点击逻辑。 ║
|
||
║ 通过约定节点名(BgClose、BtnClose),框架自动识别并绑定按钮。 ║
|
||
║ ║
|
||
║ 【使用方式】 ║
|
||
║ 1. 默认使用:在预制体中创建约定名称的按钮,无需代码 ║
|
||
║ 2. 自定义节点名:重写 DefaultCloseButtonNames 虚属性 ║
|
||
║ 3. 自定义行为:重写 OnCloseClick() 虚方法 ║
|
||
║ ║
|
||
║ 【默认行为】 ║
|
||
║ OnCloseClick() 默认执行:CloseWindow(true) - 关闭并销毁窗口 ║
|
||
║ CloseWindow(false) - 仅隐藏,保留在内存(用于频繁开关的窗口) ║
|
||
║ ║
|
||
╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
|
||
*/
|
||
|
||
/// <summary>
|
||
/// UI窗口基类 - 默认Close按钮处理部分
|
||
/// 负责自动识别和绑定预制体中的"关闭"按钮
|
||
/// </summary>
|
||
public abstract partial class UIWindow
|
||
{
|
||
#region Default Close Button - 默认关闭按钮自动绑定
|
||
|
||
/// <summary>
|
||
/// 默认关闭按钮的节点名配置(子类可重写)
|
||
/// 默认支持:BgClose(背景关闭)、BtnClose(标题栏关闭)
|
||
/// </summary>
|
||
protected virtual string[] DefaultCloseButtonNames => new[] { "BgClose", "BtnClose" };
|
||
|
||
/// <summary>
|
||
/// 绑定所有配置的默认关闭按钮
|
||
/// 在 DoInit() 阶段自动调用
|
||
/// </summary>
|
||
private void BindDefaultCloseButtons()
|
||
{
|
||
foreach (var buttonName in DefaultCloseButtonNames)
|
||
{
|
||
TryBindCloseButton(buttonName, OnCloseClick);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 尝试绑定关闭按钮(节点不存在时静默跳过)
|
||
/// </summary>
|
||
private void TryBindCloseButton(string nodeName, System.Action callback)
|
||
{
|
||
var trans = transform.Find(nodeName);
|
||
if (trans)
|
||
{
|
||
var btn = trans.GetComponent<Button>();
|
||
if (btn)
|
||
{
|
||
BindButton(btn, callback);
|
||
DebugUtil.Log("✅ 已自动绑定关闭按钮: {0} -> {1}", WindowName, nodeName);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 关闭按钮点击回调(虚方法,子类可重写)
|
||
/// 默认行为:CloseWindow(true) - 关闭并销毁窗口
|
||
/// </summary>
|
||
protected virtual void OnCloseClick()
|
||
{
|
||
CloseWindow(true);
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
|