using UnityEngine; using UnityGameFramework.Runtime; namespace SepCore.UI { public partial class ItemTooltipForm : UGuiForm { [SerializeField] private float _panelSpacing = 16f; [SerializeField] private float _screenEdgePadding = 32f; [SerializeField] private float _verticalGap = 10f; [SerializeField] private RarityPanel[] _rarityPanels = null; private ItemTooltipContext _context; private Vector3 _targetPos; public void RefreshUI(ItemTooltipContext context) { _context = context; _targetPos = ConvertWorldToAnchored(_context.TargetPos); foreach (RarityPanel panel in _rarityPanels) { panel.SetRarity(_context.BasicContext.Rarity); } // 1. 刷新基础信息面板(同步执行,立即完成文本排版与尺寸自适应) View.basicForm.RefreshUI(_context.BasicContext); // 2. 刷新扩展详情面板(Tag 羁绊 + 上一轮伤害) bool showDetail = _context.DetailContext != null && _context.DetailContext.HasAnyContent; View.detailForm.gameObject.SetActive(showDetail); if (showDetail) { View.detailForm.RefreshUI(_context.DetailContext); } // 3. 执行自适应排版定位 ApplyAdaptiveSidePositioning(); } protected override void OnOpen(object userData) { base.OnOpen(userData); if (userData is not ItemTooltipContext context) { Log.Error("ItemTooltipForm context is invalid."); return; } RefreshUI(context); } protected override void OnClose(bool isShutdown, object userData) { _context = null; base.OnClose(isShutdown, userData); } public void ApplyAdaptiveSidePositioning() { RectTransform container = View.contentRectTransform; if (container == null) { return; } RectTransform basicRect = View.basicForm.RectTransform; RectTransform detailRect = View.detailForm.RectTransform; Vector2 basicSize = basicRect.sizeDelta; if (basicSize.x <= 0f || basicSize.y <= 0f) { basicSize = basicRect.rect.size; } float basicWidth = basicSize.x > 0f ? basicSize.x : 600f; float basicHeight = basicSize.y > 0f ? basicSize.y : 500f; float halfWidth = container.rect.width * 0.5f; float halfHeight = container.rect.height * 0.5f; if (halfWidth <= 0f || halfHeight <= 0f) { if (container.parent is RectTransform parentRt) { halfWidth = parentRt.rect.width * 0.5f; halfHeight = parentRt.rect.height * 0.5f; } } // --- 纵向布局(基本面板以 pivot=(0.5, 0) 放置于目标顶部上方)--- // _targetPos 是触发悬浮的物品按钮上边缘(世界转局部坐标), // 基本面板底部对齐其上方 _verticalGap 像素,永远不覆盖物品按钮本体,杜绝 PointerExit 闪烁循环。 float basicY = _targetPos.y + _verticalGap; float maxBasicY = (halfHeight - _screenEdgePadding) - basicHeight; if (basicY > maxBasicY) { basicY = Mathf.Max(maxBasicY, _targetPos.y + _verticalGap); } bool showDetail = View.detailForm.gameObject.activeSelf; if (!showDetail) { // 无副面板:直接以目标水平中心为准,限制在屏幕边界内 float minX = -halfWidth + _screenEdgePadding + basicWidth * 0.5f; float maxX = halfWidth - _screenEdgePadding - basicWidth * 0.5f; float basicX = minX <= maxX ? Mathf.Clamp(_targetPos.x, minX, maxX) : 0f; basicRect.anchoredPosition = new Vector2(basicX, basicY); return; } // --- 双面板横向排版与左右自适应翻转 --- Vector2 detailSize = detailRect.sizeDelta; if (detailSize.x <= 0f || detailSize.y <= 0f) { detailSize = detailRect.rect.size; } float detailWidth = detailSize.x > 0f ? detailSize.x : 420f; float detailHeight = detailSize.y > 0f ? detailSize.y : 250f; float screenMinX = -halfWidth + _screenEdgePadding; float screenMaxX = halfWidth - _screenEdgePadding; float basicCenterX = _targetPos.x; float basicRight = basicCenterX + basicWidth * 0.5f; float detailRight = basicRight + _panelSpacing + detailWidth; float basicFinalX; float detailFinalX; if (detailRight <= screenMaxX) { // 1. 右侧空间充足,副面板放在右侧 float basicLeft = basicCenterX - basicWidth * 0.5f; if (basicLeft < screenMinX) { // 左侧轻微出界,向右微调 basicCenterX += (screenMinX - basicLeft); } basicFinalX = basicCenterX; float newBasicRight = basicFinalX + basicWidth * 0.5f; detailFinalX = newBasicRight + _panelSpacing + detailWidth * 0.5f; } else { // 2. 右侧空间不足,副面板自动翻转至左侧 float basicLeft = basicCenterX - basicWidth * 0.5f; float detailLeft = basicLeft - _panelSpacing - detailWidth; if (detailLeft < screenMinX) { // 翻到左侧后若超出左屏边缘,向右贴边微调 basicCenterX += (screenMinX - detailLeft); } if (basicCenterX + basicWidth * 0.5f > screenMaxX) { // 右侧也不超界 basicCenterX -= (basicCenterX + basicWidth * 0.5f - screenMaxX); } basicFinalX = basicCenterX; float newBasicLeft = basicFinalX - basicWidth * 0.5f; detailFinalX = newBasicLeft - _panelSpacing - detailWidth * 0.5f; } // --- 副面板纵向对齐(以 pivot=(0.5, 1) 顶部对齐主面板顶部)--- float basicTopEdge = basicY + basicHeight; float detailY = basicTopEdge; // 限制副面板顶部不超出屏幕顶端 if (detailY > halfHeight - _screenEdgePadding) { detailY = halfHeight - _screenEdgePadding; } // 限制副面板底端不超出屏幕底端,且不低于物品按钮顶部 float detailBottom = detailY - detailHeight; float minDetailBottom = Mathf.Max(-halfHeight + _screenEdgePadding, _targetPos.y + _verticalGap); if (detailBottom < minDetailBottom) { detailY = minDetailBottom + detailHeight; } basicRect.anchoredPosition = new Vector2(basicFinalX, basicY); detailRect.anchoredPosition = new Vector2(detailFinalX, detailY); } private Vector3 ConvertWorldToAnchored(Vector3 worldPos) { RectTransform container = View.contentRectTransform; if (container == null) { return worldPos; } Canvas canvas = container.GetComponentInParent(); Canvas rootCanvas = canvas != null ? canvas.rootCanvas : null; Camera uiCamera = null; if (rootCanvas != null && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay) { uiCamera = rootCanvas.worldCamera != null ? rootCanvas.worldCamera : Camera.main; } Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera, worldPos); if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(container, screenPoint, uiCamera, out Vector2 localPoint)) { return worldPos; } return localPoint; } } }