using UnityEngine; using System; using UnityEngine.UI; public class BattleTips : MonoBehaviour, IBattleFloatingUI { #region Inspector字段 [Header("UI Components")] public RectTransform rectTransform; public Text tipText; public Text artText; public Image background; public Image arrowUp; public Image arrowDown; [Header("Floating Config")] [Tooltip("飘字动画配置,请在Inspector中拖拽赋值")] public FloatingConfig floatingConfig; #endregion #region 公共字段 public Action OnFinish; private bool useBuffColor = false; private bool isDebuff = false; #endregion #region 私有字段 public BattleFloatingUIController controller; #endregion #region 公共方法 public void SetRatio(float speed, float scale) { EnsureControllerInitialized(); controller?.SetRatio(speed, scale); } public void SetPosition(Vector2 beginPos, Vector2 endPos) { EnsureControllerInitialized(); controller?.SetRuntimePosition(beginPos, endPos); } public void SetFloatingConfig(FloatingConfig config) { floatingConfig = config; if (controller != null) { controller.SetConfig(config); } else { InitController(); } arrowUp.SetActive(false); arrowDown.SetActive(false); } public void SetBuffColor(bool useBuff, bool isDebuffBuff) { useBuffColor = useBuff; isDebuff = isDebuffBuff; if (useBuffColor && floatingConfig != null) { Color buffColor = isDebuff ? floatingConfig.debuffColor : floatingConfig.gainBuffColor; Color beginColor = new Color(buffColor.r, buffColor.g, buffColor.b, floatingConfig.beginColor.a); Color endColor = new Color(buffColor.r, buffColor.g, buffColor.b, floatingConfig.endColor.a); EnsureControllerInitialized(); controller?.SetRuntimeColor(beginColor, endColor); arrowUp.SetActive(!isDebuff); arrowDown.SetActive(isDebuff); } } public void SetText(string text, bool useArtText = false, bool isCrit = false) { EnsureControllerInitialized(); SwitchTextDisplay(useArtText, text); Play(isCrit); } public void ShowBackground(bool show) { if (background != null) background.enabled = show; } #endregion #region IBattleFloatingUI接口实现 public void Play(bool isCrit, Action onComplete = null) { EnsureControllerInitialized(); if (controller == null) return; Action combinedCallback = () => { OnFinish?.Invoke(); OnFinish = null; onComplete?.Invoke(); }; controller.Play(isCrit, combinedCallback); } public void Run() { controller?.Run(); } public void Stop() { controller?.Stop(); } public void Resume() { controller?.Resume(); } #endregion #region 私有方法 private void InitController() { if (floatingConfig == null) { Debug.LogError($"[BattleTips] FloatingConfig 未配置! GameObject: {gameObject.name}"); return; } controller = new BattleFloatingUIController( rectTransform, gameObject, ApplyColor, floatingConfig ); SetupArrowAnchors(); } 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(); } 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); } } 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) { Color colorWithAlpha = new Color( textColor.r, textColor.g, textColor.b, floatingConfig.beginColor.a ); ApplyColor(colorWithAlpha); } } private void ApplyColor(Color color) { if (tipText.gameObject.activeSelf) tipText.color = color; if (artText.gameObject.activeSelf) artText.color = color; } #endregion }