| | |
| | | using System; |
| | | using UnityEngine.UI; |
| | | |
| | | /// <summary> |
| | | /// 战斗飘字UI组件 |
| | | /// 职责:管理UI元素和与控制器的交互 |
| | | /// </summary> |
| | | public class BattleTips : MonoBehaviour, IBattleFloatingUI |
| | | { |
| | | #region Inspector字段 |
| | |
| | | public Text tipText; |
| | | public Text artText; |
| | | public Image background; |
| | | public Image arrowUp; |
| | | public Image arrowDown; |
| | | |
| | | [Header("Floating Config")] |
| | | [Tooltip("飘字动画配置,请在Inspector中拖拽赋值")] |
| | |
| | | |
| | | public Action OnFinish; |
| | | |
| | | // Buff 颜色相关 |
| | | private bool useBuffColor = false; |
| | | private bool isDebuff = false; |
| | | |
| | |
| | | |
| | | #region 私有字段 |
| | | |
| | | // 移除 [SerializeField],controller 不应该被序列化 |
| | | public BattleFloatingUIController controller; |
| | | |
| | | #endregion |
| | | |
| | | |
| | | #region 公共方法 |
| | | |
| | | /// <summary> |
| | | /// 设置速度和缩放比例 |
| | | /// </summary> |
| | | public void SetRatio(float speed, float scale) |
| | | { |
| | | EnsureControllerInitialized(); |
| | | controller?.SetRatio(speed, scale); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 设置运行时位置(用于不跟随角色的飘字) |
| | | /// </summary> |
| | | public void SetPosition(Vector2 beginPos, Vector2 endPos) |
| | | { |
| | | EnsureControllerInitialized(); |
| | | controller?.SetRuntimePosition(beginPos, endPos); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 运行时更新配置 |
| | | /// </summary> |
| | | public void SetFloatingConfig(FloatingConfig config) |
| | | { |
| | | floatingConfig = config; |
| | |
| | | { |
| | | InitController(); |
| | | } |
| | | |
| | | arrowUp.SetActive(false); |
| | | arrowDown.SetActive(false); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 设置是否使用 Buff 颜色 |
| | | /// </summary> |
| | | public void SetBuffColor(bool useBuff, bool isDebuffBuff) |
| | | { |
| | | useBuffColor = useBuff; |
| | | isDebuff = isDebuffBuff; |
| | | |
| | | // 如果使用 buff 颜色,立即设置运行时颜色覆盖 |
| | | if (useBuffColor && floatingConfig != null) |
| | | { |
| | | Color buffColor = isDebuff ? floatingConfig.debuffColor : floatingConfig.gainBuffColor; |
| | |
| | | |
| | | EnsureControllerInitialized(); |
| | | controller?.SetRuntimeColor(beginColor, endColor); |
| | | |
| | | arrowUp.SetActive(!isDebuff); |
| | | arrowDown.SetActive(isDebuff); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 设置文本内容和样式 |
| | | /// </summary> |
| | | public void SetText(string text, bool useArtText = false, bool isCrit = false) |
| | | { |
| | | EnsureControllerInitialized(); |
| | | |
| | | // 切换文本显示类型 |
| | | SwitchTextDisplay(useArtText, text); |
| | | |
| | | // 开始播放 |
| | | Play(isCrit); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 显示/隐藏背景 |
| | | /// </summary> |
| | | public void ShowBackground(bool show) |
| | | { |
| | | if (background != null) |
| | |
| | | |
| | | if (controller == null) return; |
| | | |
| | | // 合并回调 |
| | | Action combinedCallback = () => |
| | | { |
| | | OnFinish?.Invoke(); |
| | |
| | | |
| | | #region 私有方法 |
| | | |
| | | /// <summary> |
| | | /// 初始化控制器 |
| | | /// </summary> |
| | | private void InitController() |
| | | { |
| | | if (floatingConfig == null) |
| | |
| | | ApplyColor, |
| | | floatingConfig |
| | | ); |
| | | |
| | | SetupArrowAnchors(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 确保控制器已初始化 |
| | | /// </summary> |
| | | private void SetupArrowAnchors() |
| | | { |
| | | if (arrowDown != null) |
| | | { |
| | | RectTransform arrowRect = arrowDown.rectTransform; |
| | | |
| | | if (arrowRect.parent != artText.transform) |
| | | { |
| | | arrowRect.SetParent(artText.transform, false); |
| | | } |
| | | |
| | | arrowRect.anchorMin = new Vector2(1, 0.5f); |
| | | arrowRect.anchorMax = new Vector2(1, 0.5f); |
| | | arrowRect.pivot = new Vector2(0, 0.5f); |
| | | arrowRect.anchoredPosition = new Vector2(20f, 0f); |
| | | } |
| | | |
| | | if (arrowUp != null) |
| | | { |
| | | RectTransform arrowRect = arrowUp.rectTransform; |
| | | |
| | | if (arrowRect.parent != artText.transform) |
| | | { |
| | | arrowRect.SetParent(artText.transform, false); |
| | | } |
| | | |
| | | arrowRect.anchorMin = new Vector2(1, 0.5f); |
| | | arrowRect.anchorMax = new Vector2(1, 0.5f); |
| | | arrowRect.pivot = new Vector2(0, 0.5f); |
| | | arrowRect.anchoredPosition = new Vector2(20f, 0f); |
| | | } |
| | | } |
| | | |
| | | private void EnsureControllerInitialized() |
| | | { |
| | | if (controller == null || !controller.IsValid()) |
| | | InitController(); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 切换文本显示类型 |
| | | /// </summary> |
| | | private void SwitchTextDisplay(bool useArtText, string text) |
| | | { |
| | | if (useArtText) |
| | |
| | | artText.text = text; |
| | | tipText.gameObject.SetActive(false); |
| | | artText.gameObject.SetActive(true); |
| | | SwitchArrowParent(artText.rectTransform); |
| | | } |
| | | else |
| | | { |
| | | tipText.text = text; |
| | | artText.gameObject.SetActive(false); |
| | | tipText.gameObject.SetActive(true); |
| | | SwitchArrowParent(tipText.rectTransform); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 应用文本颜色(保留配置的透明度) |
| | | /// </summary> |
| | | private void SwitchArrowParent(RectTransform newParent) |
| | | { |
| | | if (arrowDown != null && arrowDown.rectTransform.parent != newParent) |
| | | { |
| | | arrowDown.rectTransform.SetParent(newParent, false); |
| | | arrowDown.rectTransform.anchorMin = new Vector2(1, 0.5f); |
| | | arrowDown.rectTransform.anchorMax = new Vector2(1, 0.5f); |
| | | arrowDown.rectTransform.pivot = new Vector2(0, 0.5f); |
| | | arrowDown.rectTransform.anchoredPosition = new Vector2(5, 0); |
| | | } |
| | | |
| | | if (arrowUp != null && arrowUp.rectTransform.parent != newParent) |
| | | { |
| | | arrowUp.rectTransform.SetParent(newParent, false); |
| | | arrowUp.rectTransform.anchorMin = new Vector2(1, 0.5f); |
| | | arrowUp.rectTransform.anchorMax = new Vector2(1, 0.5f); |
| | | arrowUp.rectTransform.pivot = new Vector2(0, 0.5f); |
| | | arrowUp.rectTransform.anchoredPosition = new Vector2(5, 0); |
| | | } |
| | | } |
| | | |
| | | private void ApplyTextColor(Color textColor) |
| | | { |
| | | if (floatingConfig != null) |
| | |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 应用颜色到激活的文本组件 |
| | | /// </summary> |
| | | private void ApplyColor(Color color) |
| | | { |
| | | if (tipText.gameObject.activeSelf) |