2025-09-04 14:23:51 +08:00
|
|
|
|
using System;
|
2025-12-27 16:34:21 +08:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
2025-09-04 14:23:51 +08:00
|
|
|
|
using Framework;
|
|
|
|
|
|
using PhxhSDK;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/*
|
|
|
|
|
|
╔═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
|
|
|
|
|
|
║ UIWindow 按钮绑定模块 (Button Binding) ║
|
|
|
|
|
|
╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
|
|
|
|
|
|
║ ║
|
|
|
|
|
|
║ 【模块功能】 ║
|
2025-12-29 14:43:15 +08:00
|
|
|
|
║ 本模块提供按钮绑定的核心功能: ║
|
|
|
|
|
|
║ • 手动按钮绑定 API - 在OnInit()中手动绑定自定义按钮 ║
|
2025-12-29 13:54:47 +08:00
|
|
|
|
║ ║
|
2025-12-29 14:43:15 +08:00
|
|
|
|
║ 注意:默认按钮的处理已移至独立的 partial 文件: ║
|
2025-12-29 13:54:47 +08:00
|
|
|
|
║ • UIWindow.DefaultCloseButton.cs - 处理默认关闭按钮(BgClose、BtnClose) ║
|
|
|
|
|
|
║ • UIWindow.DefaultHomeButton.cs - 处理默认回主界面按钮(BtnHome、BtnBackHome、HomeButton) ║
|
2025-12-29 14:43:15 +08:00
|
|
|
|
║ • UIWindow.DefaultBackButton.cs - 处理默认返回按钮(BtnBack、BackButton、BtnReturn) ║
|
2025-12-27 16:34:21 +08:00
|
|
|
|
║ ║
|
|
|
|
|
|
║ 【使用方式】 ║
|
|
|
|
|
|
║ ║
|
|
|
|
|
|
║ 在 OnInit() 中使用 BindButton() API 手动绑定: ║
|
|
|
|
|
|
║ ║
|
|
|
|
|
|
║ 示例: ║
|
|
|
|
|
|
║ protected override void OnInit() ║
|
|
|
|
|
|
║ { ║
|
|
|
|
|
|
║ // 通过路径绑定 ║
|
|
|
|
|
|
║ BindButton("BtnStart", OnStartClick); ║
|
|
|
|
|
|
║ ║
|
|
|
|
|
|
║ // 通过Button组件绑定 ║
|
|
|
|
|
|
║ BindButton(myButton, OnMyButtonClick); ║
|
|
|
|
|
|
║ ║
|
|
|
|
|
|
║ // 带参数绑定 ║
|
|
|
|
|
|
║ BindButton(itemButton, OnItemClick, itemData); ║
|
|
|
|
|
|
║ ║
|
|
|
|
|
|
║ // 静音绑定(不播放音效) ║
|
|
|
|
|
|
║ BindButton(silentButton, OnSilentClick, playAudio: false); ║
|
|
|
|
|
|
║ } ║
|
|
|
|
|
|
║ ║
|
|
|
|
|
|
║ 【更多信息】 ║
|
2025-12-29 14:43:15 +08:00
|
|
|
|
║ • 关闭按钮:UIWindow.DefaultCloseButton.cs ║
|
|
|
|
|
|
║ • 回主界面按钮:UIWindow.DefaultHomeButton.cs ║
|
|
|
|
|
|
║ • 返回按钮:UIWindow.DefaultBackButton.cs ║
|
2025-12-27 16:34:21 +08:00
|
|
|
|
║ ║
|
|
|
|
|
|
╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-09-04 14:23:51 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// UI窗口基类 - 按钮绑定部分
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// 提供按钮绑定API和默认按钮自动绑定功能
|
2025-09-04 14:23:51 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract partial class UIWindow
|
|
|
|
|
|
{
|
2025-12-27 16:34:21 +08:00
|
|
|
|
#region Button Binding Core - 按钮绑定核心逻辑
|
|
|
|
|
|
|
2025-09-04 14:23:51 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 处理按钮点击的音频逻辑
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void HandleButtonAudio(Button button, bool playAudio)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!playAudio) return;
|
|
|
|
|
|
|
|
|
|
|
|
var clickAudio = button.GetComponent<ClickAudio>();
|
|
|
|
|
|
if (clickAudio)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (clickAudio.IsPlayAudio && !string.IsNullOrEmpty(clickAudio.AudioKey))
|
|
|
|
|
|
{
|
|
|
|
|
|
AudioManager.Instance.PlayAudio(clickAudio.AudioKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
CommonUtilsFramework.PlayUICommonSound();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// 绑定按钮点击事件的核心逻辑(无参数)
|
2025-09-04 14:23:51 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void BindButtonCore(Button button, Action action, bool playAudio = true)
|
|
|
|
|
|
{
|
2025-10-23 15:43:50 +08:00
|
|
|
|
if (button is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("传入按钮为空,无法绑定点击事件!");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-09-04 14:23:51 +08:00
|
|
|
|
|
|
|
|
|
|
button.onClick.RemoveAllListeners();
|
|
|
|
|
|
button.onClick.AddListener(() =>
|
|
|
|
|
|
{
|
2025-10-23 15:43:50 +08:00
|
|
|
|
DebugUtil.Log("按钮被点击: {0}", button.gameObject.name);
|
2025-09-12 15:18:50 +08:00
|
|
|
|
if (UIManager.IsUIInputBlocked) return;
|
2025-09-04 14:23:51 +08:00
|
|
|
|
|
2025-09-12 15:18:50 +08:00
|
|
|
|
UIManager.Instance.BlockUIForNormalOperation();
|
2025-09-04 14:23:51 +08:00
|
|
|
|
HandleButtonAudio(button, playAudio);
|
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
|
});
|
2025-10-23 15:43:50 +08:00
|
|
|
|
|
|
|
|
|
|
DebugUtil.Log("按钮绑定完成: {0}", button.gameObject.name);
|
2025-09-04 14:23:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 绑定按钮点击事件的核心逻辑(带参数)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void BindButtonCore<T>(Button button, Action<T> action, T parameter, bool playAudio = true)
|
|
|
|
|
|
{
|
2025-09-13 16:51:11 +08:00
|
|
|
|
if (button is null) return;
|
2025-09-04 14:23:51 +08:00
|
|
|
|
|
|
|
|
|
|
button.onClick.RemoveAllListeners();
|
|
|
|
|
|
button.onClick.AddListener(() =>
|
|
|
|
|
|
{
|
2025-09-12 15:18:50 +08:00
|
|
|
|
if (UIManager.IsUIInputBlocked) return;
|
2025-09-04 14:23:51 +08:00
|
|
|
|
|
2025-09-12 15:18:50 +08:00
|
|
|
|
UIManager.Instance.BlockUIForNormalOperation();
|
2025-09-04 14:23:51 +08:00
|
|
|
|
HandleButtonAudio(button, playAudio);
|
|
|
|
|
|
|
|
|
|
|
|
if (parameter == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.LogError("=== 传入参数为空! ===");
|
|
|
|
|
|
}
|
|
|
|
|
|
action?.Invoke(parameter);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2025-12-27 16:34:21 +08:00
|
|
|
|
#region Button Binding Public API - 手动绑定按钮的公共API
|
2025-09-04 14:23:51 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通过路径绑定按钮
|
|
|
|
|
|
/// </summary>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// <param name="target">按钮节点路径(相对于窗口根节点)</param>
|
2025-09-04 14:23:51 +08:00
|
|
|
|
/// <param name="action">点击回调</param>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// <param name="playAudio">是否播放点击音效(默认true)</param>
|
|
|
|
|
|
/// <returns>Button组件</returns>
|
|
|
|
|
|
/// <example>
|
|
|
|
|
|
/// BindButton("TopBar/BtnSettings", OnSettingsClick);
|
|
|
|
|
|
/// </example>
|
2025-09-04 14:23:51 +08:00
|
|
|
|
public Button BindButton(string target, Action action, bool playAudio = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
GameObject obj = transform.Find(target)?.gameObject;
|
2025-09-13 16:51:11 +08:00
|
|
|
|
if (obj is not null)
|
2025-09-04 14:23:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
Button button = obj.GetComponent<Button>();
|
|
|
|
|
|
BindButtonCore(button, action, playAudio);
|
|
|
|
|
|
return button;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-12-27 16:34:21 +08:00
|
|
|
|
DebugUtil.LogError("未找到按钮节点: {0}/{1}", gameObject.name, target);
|
2025-09-04 14:23:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// 直接绑定Button组件
|
2025-09-04 14:23:51 +08:00
|
|
|
|
/// </summary>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// <param name="button">Button组件</param>
|
2025-09-04 14:23:51 +08:00
|
|
|
|
/// <param name="action">点击回调</param>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// <param name="playAudio">是否播放点击音效(默认true)</param>
|
|
|
|
|
|
/// <returns>Button组件</returns>
|
|
|
|
|
|
/// <example>
|
|
|
|
|
|
/// BindButton(myButton, OnMyButtonClick);
|
|
|
|
|
|
/// </example>
|
|
|
|
|
|
public Button BindButton(Button button, Action action, bool playAudio = true)
|
2025-09-04 14:23:51 +08:00
|
|
|
|
{
|
2025-12-27 16:34:21 +08:00
|
|
|
|
if (button is not null)
|
2025-09-04 14:23:51 +08:00
|
|
|
|
{
|
2025-12-27 16:34:21 +08:00
|
|
|
|
BindButtonCore(button, action, playAudio);
|
2025-09-04 14:23:51 +08:00
|
|
|
|
return button;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-12-27 16:34:21 +08:00
|
|
|
|
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!");
|
2025-09-04 14:23:51 +08:00
|
|
|
|
}
|
2025-12-27 16:34:21 +08:00
|
|
|
|
return button;
|
2025-09-04 14:23:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// 通过GameObject绑定按钮(带参数)
|
2025-09-04 14:23:51 +08:00
|
|
|
|
/// </summary>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// <typeparam name="T">参数类型</typeparam>
|
|
|
|
|
|
/// <param name="obj">按钮GameObject</param>
|
2025-09-04 14:23:51 +08:00
|
|
|
|
/// <param name="action">点击回调</param>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// <param name="parameter">回调参数</param>
|
|
|
|
|
|
/// <param name="playAudio">是否播放点击音效(默认true)</param>
|
|
|
|
|
|
/// <returns>Button组件</returns>
|
|
|
|
|
|
/// <example>
|
|
|
|
|
|
/// BindButton(itemObj, OnItemClick, itemData);
|
|
|
|
|
|
/// </example>
|
|
|
|
|
|
public Button BindButton<T>(GameObject obj, Action<T> action, T parameter, bool playAudio = true)
|
2025-09-04 14:23:51 +08:00
|
|
|
|
{
|
2025-12-27 16:34:21 +08:00
|
|
|
|
if (obj is not null)
|
2025-09-04 14:23:51 +08:00
|
|
|
|
{
|
2025-12-27 16:34:21 +08:00
|
|
|
|
Button button = obj.GetComponent<Button>();
|
|
|
|
|
|
BindButtonCore(button, action, parameter, playAudio);
|
2025-09-04 14:23:51 +08:00
|
|
|
|
return button;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-12-27 16:34:21 +08:00
|
|
|
|
DebugUtil.LogError("传入GameObject为空,请检查按钮绑定参数!");
|
2025-09-04 14:23:51 +08:00
|
|
|
|
}
|
2025-12-27 16:34:21 +08:00
|
|
|
|
return null;
|
2025-09-04 14:23:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 直接绑定Button组件(带参数)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">参数类型</typeparam>
|
|
|
|
|
|
/// <param name="button">Button组件</param>
|
|
|
|
|
|
/// <param name="action">点击回调</param>
|
|
|
|
|
|
/// <param name="parameter">回调参数</param>
|
2025-12-27 16:34:21 +08:00
|
|
|
|
/// <param name="playAudio">是否播放点击音效(默认true)</param>
|
|
|
|
|
|
/// <returns>Button组件</returns>
|
|
|
|
|
|
/// <example>
|
|
|
|
|
|
/// BindButton(itemButton, OnItemClick, itemData);
|
|
|
|
|
|
/// </example>
|
2025-09-04 14:23:51 +08:00
|
|
|
|
public Button BindButton<T>(Button button, Action<T> action, T parameter, bool playAudio = true)
|
|
|
|
|
|
{
|
2025-09-13 16:51:11 +08:00
|
|
|
|
if (button is not null)
|
2025-09-04 14:23:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
BindButtonCore(button, action, parameter, playAudio);
|
|
|
|
|
|
return button;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-12-27 16:34:21 +08:00
|
|
|
|
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!");
|
2025-09-04 14:23:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
return button;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|