153 lines
4.6 KiB
C#
153 lines
4.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream.MiniGame.Language
|
|
{
|
|
/// <summary>
|
|
/// 分析模式解码器条纹组件(可拖拽)
|
|
/// </summary>
|
|
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<RectTransform>();
|
|
}
|
|
|
|
if (canvasGroup == null)
|
|
{
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
|
if (canvasGroup == null)
|
|
{
|
|
canvasGroup = gameObject.AddComponent<CanvasGroup>();
|
|
}
|
|
}
|
|
|
|
if (backgroundImage == null)
|
|
{
|
|
backgroundImage = GetComponent<Image>();
|
|
}
|
|
|
|
// 设置样式
|
|
if (backgroundImage != null)
|
|
{
|
|
backgroundImage.color = stripColor;
|
|
}
|
|
|
|
// 添加边框(使用 Outline 组件)
|
|
Outline outline = GetComponent<Outline>();
|
|
if (outline == null)
|
|
{
|
|
outline = gameObject.AddComponent<Outline>();
|
|
}
|
|
outline.effectColor = borderColor;
|
|
outline.effectDistance = new Vector2(borderWidth, borderWidth);
|
|
|
|
originalAlpha = canvasGroup.alpha;
|
|
|
|
// 获取父容器
|
|
if (transform.parent != null)
|
|
{
|
|
parentRectTransform = transform.parent.GetComponent<RectTransform>();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置是否可拖拽
|
|
/// </summary>
|
|
public void SetDraggable(bool draggable)
|
|
{
|
|
isDraggable = draggable;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前位置(网格坐标)
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置网格位置
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|