using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace AibisDream.MiniGame.Language { /// /// 分析模式解码器条纹组件(可拖拽) /// public class AnalysisDecoderStrip : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { [Header("组件")] [SerializeField] private RectTransform rectTransform; [SerializeField] private CanvasGroup canvasGroup; [SerializeField] private Image backgroundImage; [Header("拖拽配置")] [SerializeField] private bool isDraggable = true; [SerializeField] private float dragAlpha = 0.7f; [Header("样式配置")] [SerializeField] private Color stripColor = new Color(0f, 0.12f, 0.12f, 0.95f); [SerializeField] private Color borderColor = Color.cyan; [SerializeField] private float borderWidth = 2f; private Vector2 dragOffset; private RectTransform parentRectTransform; private float originalAlpha = 1f; private void Awake() { if (rectTransform == null) { rectTransform = GetComponent(); } if (canvasGroup == null) { canvasGroup = GetComponent(); if (canvasGroup == null) { canvasGroup = gameObject.AddComponent(); } } if (backgroundImage == null) { backgroundImage = GetComponent(); } // 设置样式 if (backgroundImage != null) { backgroundImage.color = stripColor; } // 添加边框(使用 Outline 组件) Outline outline = GetComponent(); if (outline == null) { outline = gameObject.AddComponent(); } outline.effectColor = borderColor; outline.effectDistance = new Vector2(borderWidth, borderWidth); originalAlpha = canvasGroup.alpha; // 获取父容器 if (transform.parent != null) { parentRectTransform = transform.parent.GetComponent(); } } public void OnBeginDrag(PointerEventData eventData) { if (!isDraggable) return; // 计算拖拽偏移 RectTransformUtility.ScreenPointToLocalPointInRectangle( parentRectTransform ?? rectTransform, eventData.position, eventData.pressEventCamera, out Vector2 localPoint); dragOffset = rectTransform.anchoredPosition - localPoint; // 降低透明度 if (canvasGroup != null) { canvasGroup.alpha = dragAlpha; } } public void OnDrag(PointerEventData eventData) { if (!isDraggable) return; // 更新位置 if (RectTransformUtility.ScreenPointToLocalPointInRectangle( parentRectTransform ?? rectTransform, eventData.position, eventData.pressEventCamera, out Vector2 localPoint)) { rectTransform.anchoredPosition = localPoint + dragOffset; } } public void OnEndDrag(PointerEventData eventData) { if (!isDraggable) return; // 恢复透明度 if (canvasGroup != null) { canvasGroup.alpha = originalAlpha; } } /// /// 设置是否可拖拽 /// public void SetDraggable(bool draggable) { isDraggable = draggable; } /// /// 获取当前位置(网格坐标) /// public Vector2Int GetGridPosition(float cellSize, float cellGap) { float unit = cellSize + cellGap; int col = Mathf.RoundToInt(rectTransform.anchoredPosition.x / unit); int row = Mathf.RoundToInt(-rectTransform.anchoredPosition.y / unit); return new Vector2Int(row, col); } /// /// 设置网格位置 /// public void SetGridPosition(int row, int col, float cellSize, float cellGap) { float unit = cellSize + cellGap; float xPos = col * unit; float yPos = -row * unit; rectTransform.anchoredPosition = new Vector2(xPos, yPos); } } }