184 lines
5.6 KiB
C#
184 lines
5.6 KiB
C#
using System;
|
||
using Framework;
|
||
using PhxhSDK;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
/// <summary>
|
||
/// UI窗口基类 - 按钮绑定部分
|
||
/// 包含按钮事件绑定、音频处理等功能
|
||
/// </summary>
|
||
public abstract partial class UIWindow
|
||
{
|
||
#region Button Binding Core
|
||
|
||
/// <summary>
|
||
/// 处理按钮点击的音频逻辑
|
||
/// </summary>
|
||
/// <param name="button">按钮组件</param>
|
||
/// <param name="playAudio">是否播放音频</param>
|
||
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>
|
||
/// 绑定按钮点击事件的核心逻辑
|
||
/// </summary>
|
||
/// <param name="button">按钮组件</param>
|
||
/// <param name="action">点击回调</param>
|
||
/// <param name="playAudio">是否播放音频</param>
|
||
private void BindButtonCore(Button button, Action action, bool playAudio = true)
|
||
{
|
||
if (button == null) return;
|
||
|
||
button.onClick.RemoveAllListeners();
|
||
button.onClick.AddListener(() =>
|
||
{
|
||
if (UIManager.IsClickDisabled) return;
|
||
|
||
UIManager.Instance.OnClick();
|
||
HandleButtonAudio(button, playAudio);
|
||
action?.Invoke();
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绑定按钮点击事件的核心逻辑(带参数)
|
||
/// </summary>
|
||
/// <typeparam name="T">参数类型</typeparam>
|
||
/// <param name="button">按钮组件</param>
|
||
/// <param name="action">点击回调</param>
|
||
/// <param name="parameter">参数</param>
|
||
/// <param name="playAudio">是否播放音频</param>
|
||
private void BindButtonCore<T>(Button button, Action<T> action, T parameter, bool playAudio = true)
|
||
{
|
||
if (button == null) return;
|
||
|
||
button.onClick.RemoveAllListeners();
|
||
button.onClick.AddListener(() =>
|
||
{
|
||
if (UIManager.IsClickDisabled) return;
|
||
|
||
UIManager.Instance.OnClick();
|
||
HandleButtonAudio(button, playAudio);
|
||
|
||
if (parameter == null)
|
||
{
|
||
DebugUtil.LogError("=== 传入参数为空! ===");
|
||
}
|
||
action?.Invoke(parameter);
|
||
});
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Button Binding Public Interface
|
||
|
||
/// <summary>
|
||
/// 通过路径绑定按钮
|
||
/// </summary>
|
||
/// <param name="target">按钮路径</param>
|
||
/// <param name="action">点击回调</param>
|
||
/// <param name="playAudio">是否播放音频</param>
|
||
/// <returns>找到的GameObject</returns>
|
||
public Button BindButton(string target, Action action, bool playAudio = true)
|
||
{
|
||
GameObject obj = transform.Find(target)?.gameObject;
|
||
if (obj != null)
|
||
{
|
||
Button button = obj.GetComponent<Button>();
|
||
BindButtonCore(button, action, playAudio);
|
||
return button;
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("未找到 {0}/{1}", gameObject.name, target);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通过GameObject绑定按钮(带参数)
|
||
/// </summary>
|
||
/// <typeparam name="T">参数类型</typeparam>
|
||
/// <param name="obj">按钮GameObject</param>
|
||
/// <param name="action">点击回调</param>
|
||
/// <param name="parameter">回调参数</param>
|
||
/// <param name="playAudio">是否播放音频</param>
|
||
/// <returns>传入的GameObject</returns>
|
||
public Button BindButton<T>(GameObject obj, Action<T> action, T parameter, bool playAudio = true)
|
||
{
|
||
if (obj != null)
|
||
{
|
||
Button button = obj.GetComponent<Button>();
|
||
BindButtonCore(button, action, parameter, playAudio);
|
||
return button;
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("传入GameObject为空,请检查按钮绑定参数!!!");
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 直接绑定Button组件
|
||
/// </summary>
|
||
/// <param name="button">Button组件</param>
|
||
/// <param name="action">点击回调</param>
|
||
/// <param name="playAudio">是否播放音频</param>
|
||
/// <returns>Button的GameObject</returns>
|
||
public Button BindButton(Button button, Action action, bool playAudio = true)
|
||
{
|
||
if (button != null)
|
||
{
|
||
BindButtonCore(button, action, playAudio);
|
||
return button;
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!!!");
|
||
}
|
||
return button;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 直接绑定Button组件(带参数)
|
||
/// </summary>
|
||
/// <typeparam name="T">参数类型</typeparam>
|
||
/// <param name="button">Button组件</param>
|
||
/// <param name="action">点击回调</param>
|
||
/// <param name="parameter">回调参数</param>
|
||
/// <param name="playAudio">是否播放音频</param>
|
||
/// <returns>Button的GameObject</returns>
|
||
public Button BindButton<T>(Button button, Action<T> action, T parameter, bool playAudio = true)
|
||
{
|
||
if (button != null)
|
||
{
|
||
BindButtonCore(button, action, parameter, playAudio);
|
||
return button;
|
||
}
|
||
else
|
||
{
|
||
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!!!");
|
||
}
|
||
return button;
|
||
}
|
||
|
||
#endregion
|
||
}
|