214 lines
6.4 KiB
C#
214 lines
6.4 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
|
||
/// <summary>
|
||
/// 自适应按钮组件
|
||
/// 功能:根据文本内容自动调整按钮宽度
|
||
/// 使用方法:将此脚本附加到包含TextMeshProUGUI组件的按钮上,然后调用SetText方法设置文本
|
||
/// </summary>
|
||
public class AdaptiveButton : MonoBehaviour
|
||
{
|
||
// 配置参数
|
||
[Header("宽度限制设置")]
|
||
[SerializeField] private float minWidth = 50f; // 最小宽度
|
||
[SerializeField] private float maxWidth = 500f; // 最大宽度
|
||
[SerializeField] private float padding = 20f; // 内边距补偿
|
||
|
||
[Header("调试信息")]
|
||
[SerializeField] private bool enableDebugLog = true; // 是否启用调试日志
|
||
|
||
// 缓存的组件引用
|
||
private TextMeshProUGUI textComponent; // 文本组件
|
||
private RectTransform buttonRectTransform; // 按钮的RectTransform
|
||
private Button buttonComponent; // 按钮组件
|
||
|
||
/// <summary>
|
||
/// 初始化组件
|
||
/// </summary>
|
||
private void Awake()
|
||
{
|
||
// 获取按钮组件
|
||
buttonComponent = GetComponent<Button>();
|
||
if (buttonComponent == null)
|
||
{
|
||
Debug.LogError($"[AdaptiveButton] 在对象 {gameObject.name} 上未找到Button组件!");
|
||
return;
|
||
}
|
||
|
||
// 获取RectTransform组件
|
||
buttonRectTransform = GetComponent<RectTransform>();
|
||
if (buttonRectTransform == null)
|
||
{
|
||
Debug.LogError($"[AdaptiveButton] 在对象 {gameObject.name} 上未找到RectTransform组件!");
|
||
return;
|
||
}
|
||
|
||
// 查找文本组件
|
||
FindTextComponent();
|
||
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"[AdaptiveButton] 组件初始化完成 - 对象: {gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查找并缓存TextMeshProUGUI组件
|
||
/// </summary>
|
||
private void FindTextComponent()
|
||
{
|
||
// 在子对象中查找TextMeshProUGUI组件
|
||
textComponent = GetComponentInChildren<TextMeshProUGUI>();
|
||
|
||
if (textComponent == null)
|
||
{
|
||
Debug.LogError($"[AdaptiveButton] 在按钮 {gameObject.name} 的子对象中未找到TextMeshProUGUI组件!");
|
||
return;
|
||
}
|
||
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"[AdaptiveButton] 找到文本组件 - 对象: {textComponent.gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调整按钮宽度以适应文本内容
|
||
/// </summary>
|
||
private void AdjustButtonWidth()
|
||
{
|
||
if (textComponent == null)
|
||
{
|
||
Debug.LogError($"[AdaptiveButton] 文本组件为空,无法调整宽度!");
|
||
return;
|
||
}
|
||
|
||
// 强制更新文本布局
|
||
textComponent.ForceMeshUpdate();
|
||
|
||
// 获取文本的实际宽度
|
||
float textWidth = textComponent.preferredWidth;
|
||
|
||
// 计算目标宽度(文本宽度 + 内边距)
|
||
float targetWidth = textWidth + padding;
|
||
|
||
// 应用最小/最大宽度限制
|
||
targetWidth = Mathf.Clamp(targetWidth, minWidth, maxWidth);
|
||
|
||
// 调整按钮宽度
|
||
Vector2 currentSize = buttonRectTransform.sizeDelta;
|
||
currentSize.x = targetWidth;
|
||
buttonRectTransform.sizeDelta = currentSize;
|
||
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"[AdaptiveButton] 按钮宽度已调整 - 文本宽度: {textWidth:F1}, 目标宽度: {targetWidth:F1}, 对象: {gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置按钮文本并自动调整宽度
|
||
/// </summary>
|
||
/// <param name="text">要设置的文本内容</param>
|
||
public void SetText(string text)
|
||
{
|
||
if (textComponent == null)
|
||
{
|
||
Debug.LogError($"[AdaptiveButton] 文本组件为空,无法设置文本!");
|
||
return;
|
||
}
|
||
|
||
// 设置文本内容
|
||
textComponent.text = text;
|
||
|
||
// 调整按钮宽度
|
||
AdjustButtonWidth();
|
||
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"[AdaptiveButton] 文本已设置 - 内容: \"{text}\", 对象: {gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 静态方法:为指定按钮设置文本并调整宽度
|
||
/// </summary>
|
||
/// <param name="button">目标按钮</param>
|
||
/// <param name="text">要设置的文本内容</param>
|
||
public static void SetButtonText(Button button, string text)
|
||
{
|
||
if (button == null)
|
||
{
|
||
Debug.LogError("[AdaptiveButton] 按钮参数为空!");
|
||
return;
|
||
}
|
||
|
||
// 获取AdaptiveButton组件
|
||
AdaptiveButton adaptiveButton = button.GetComponent<AdaptiveButton>();
|
||
|
||
if (adaptiveButton == null)
|
||
{
|
||
Debug.LogError($"[AdaptiveButton] 按钮 {button.gameObject.name} 上没有AdaptiveButton组件!");
|
||
return;
|
||
}
|
||
|
||
// 调用SetText方法
|
||
adaptiveButton.SetText(text);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前文本内容
|
||
/// </summary>
|
||
/// <returns>当前文本内容</returns>
|
||
public string GetText()
|
||
{
|
||
if (textComponent == null)
|
||
{
|
||
Debug.LogError("[AdaptiveButton] 文本组件为空!");
|
||
return string.Empty;
|
||
}
|
||
|
||
return textComponent.text;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置最小宽度
|
||
/// </summary>
|
||
/// <param name="width">最小宽度值</param>
|
||
public void SetMinWidth(float width)
|
||
{
|
||
minWidth = width;
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"[AdaptiveButton] 最小宽度已设置为: {width}, 对象: {gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置最大宽度
|
||
/// </summary>
|
||
/// <param name="width">最大宽度值</param>
|
||
public void SetMaxWidth(float width)
|
||
{
|
||
maxWidth = width;
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"[AdaptiveButton] 最大宽度已设置为: {width}, 对象: {gameObject.name}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置内边距
|
||
/// </summary>
|
||
/// <param name="paddingValue">内边距值</param>
|
||
public void SetPadding(float paddingValue)
|
||
{
|
||
padding = paddingValue;
|
||
if (enableDebugLog)
|
||
{
|
||
Debug.Log($"[AdaptiveButton] 内边距已设置为: {paddingValue}, 对象: {gameObject.name}");
|
||
}
|
||
}
|
||
}
|